Skip to content

Commit

Permalink
feat: add readonly ranges to chapter 4
Browse files Browse the repository at this point in the history
  • Loading branch information
LyonSyonII committed Nov 17, 2024
1 parent 40d6897 commit d24d752
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
10 changes: 5 additions & 5 deletions frontend/src/content/questions/4-1.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { type CodeQuestion, type Validator, codeMess, getAnswer, replace } from "./CodeQuestion";
import { type CodeQuestion, type Validator, codeMess, getAnswer, mc, mo, replace } from "./CodeQuestion";

const code = `
let name = "$NAME";
let surname = ?;
let mut age = ?;
let name = "${mo}$NAME${mc}";
let surname = ${mo}?${mc};
let mut age = ${mo}?${mc};
`;

const setup = `
Expand Down Expand Up @@ -31,7 +31,7 @@ export const question: CodeQuestion = {
setup,
vars: [{ v: "NAME", d: "Hero" }],
onsuccess: (_, value) => {
const name = getAnswer("name = ", value).slice(1, -1);
const name = getAnswer("name = ", value).slice(2, -2);
localStorage.setItem("NAME", name);
},
validator,
Expand Down
11 changes: 6 additions & 5 deletions frontend/src/content/questions/4-2.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { type CodeQuestion, type Validator, codeMessQuestion, getAnswer, replace } from "./CodeQuestion";
import { type CodeQuestion, type Validator, codeMessQuestion, getAnswer, mc, mo, replace } from "./CodeQuestion";

// TODO: Vertical cursor navigation is inconsistent
const code = `
let mut height = ?;
let mut weight = ?;
let mut money = 12.50;
let mut height = ${mo}?${mc};
let mut weight = ${mo}?${mc};
let mut money = ${mo}12.50${mc};
`;

const setup = `
__VALUE__
println!("Your height is {height} meters, your weight is {weight} kilograms and you have {money} coins.SUCCESS");
println!("You measure {height} meters, you weight {weight} kilograms and you have {money} coins.SUCCESS");
`;

const validator: Validator = (value, test) => {
Expand Down
10 changes: 5 additions & 5 deletions frontend/src/content/questions/4-3.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { createRegExp, word } from "magic-regexp";
import { type CodeQuestion, type Validator, codeMessQuestion, replace } from "./CodeQuestion";
import { type CodeQuestion, type Validator, codeMessQuestion, mc, mo, replace } from "./CodeQuestion";
import { _, char, end, semicolon, start, stringZ, wrongCharZ } from "./regex";

const code = `
let initial1 = '$NAME';
let initial2 = ?;
let mut cardinal = ?;
let initial1 = '${mo}$NAME${mc}';
let initial2 = ${mo}?${mc};
let mut cardinal = ${mo}?${mc};
`;

const setup = `
Expand Down Expand Up @@ -62,7 +62,7 @@ export const question: CodeQuestion = {
vars: [{
v: "NAME",
d: "Hero",
c: (v) => v[0]?.toUpperCase()
c: (v) => v[0]?.toUpperCase() || "H"
}],
validator,
};
13 changes: 7 additions & 6 deletions frontend/src/content/questions/4-4.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { createRegExp } from "magic-regexp";
import { type CodeQuestion, type Validator, codeMessQuestion } from "./CodeQuestion";
import { type CodeQuestion, type Validator, codeMessQuestion, mc, mo } from "./CodeQuestion";
import { _, any, bool, end, semicolon, start } from "./regex";

const code = `
let is_human = true;
let mut registered = false;
let mut dead = ?;
let mut wears_glasses = ?;
let is_human = ${mo}true${mc};
let mut registered = ${mo}false${mc};
let mut dead = ${mo}?${mc};
let mut wears_glasses = ${mo}?${mc};
`;

const setup = `
__VALUE__;
let glasses = match wears_glasses {
true => "And I also wear glasses, we match!",
true => "And I also wear glasses, we match 😉",
false => "And good! As an adventurer, it's better if you don't need glasses."
};
println!("I'm glad you're not dead!\\n{glasses}\\nSUCCESS");
Expand Down Expand Up @@ -48,6 +48,7 @@ const validator: Validator = (value) => {
return dead.includes("?") && "[dead] Are you dead or alive?"
|| dead === "true" && "[dead] Are you sure you're dead? How are you answering this question?"
|| dead !== "false" && `[dead]${wrong}`
|| registered === "true" && "[registered] You're not registered yet!"
|| glasses.includes("?") && "[wears_glasses] Do you wear glasses?"
|| !(/true|false/).test(glasses) && `[wears_glasses]${wrong}`
|| undefined
Expand Down

0 comments on commit d24d752

Please sign in to comment.