Skip to content

Commit

Permalink
Prettified Code!
Browse files Browse the repository at this point in the history
  • Loading branch information
timhaller authored and actions-user committed Nov 30, 2023
1 parent 827189d commit 1b4f8d3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 48 deletions.
31 changes: 17 additions & 14 deletions exercices/m05/ex9/main.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
const essais = Math.min(askForNumber("Entrez le nombre d'essais: (1 à 3)", "N"), 3);
const essais = Math.min(
askForNumber("Entrez le nombre d'essais: (1 à 3)", "N"),
3,
);

const maxNumber = essais * 5
const maxNumber = essais * 5;
const secretNumber = lancerDe(maxNumber);
let message = "Trouvez le nombre secret entre 1 et " + maxNumber + "\n";

function guess() {
for (let i = 1; i <= essais; i++) {
const guess = askForNumber(message + "Essai " + i + ": ", "Z");
if (guess === secretNumber) {
return true;
} else if (guess < secretNumber) {
message = "Trop petit!\n";
} else {
message = "Trop grand!\n";
}
for (let i = 1; i <= essais; i++) {
const guess = askForNumber(message + "Essai " + i + ": ", "Z");
if (guess === secretNumber) {
return true;
} else if (guess < secretNumber) {
message = "Trop petit!\n";
} else {
message = "Trop grand!\n";
}
return false;
}
return false;
}

if (guess()) {
alert("Bravo!");
alert("Bravo!");
} else {
alert("Perdu! Le nombre était " + secretNumber);
alert("Perdu! Le nombre était " + secretNumber);
}
68 changes: 34 additions & 34 deletions functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,51 @@

//alert
function customAlert(text = "") {
terminalOutput.innerHTML += `<p>${text}</p>`;
terminalOutput.innerHTML += `<p>${text}</p>`;
}

//since window.alert is a function, we can override it
window.alert = customAlert;

//prompt
function prompt(text = "") {
return new Promise((resolve) => {
terminalOutput.innerHTML += `<span id="promptLine"><span>${text} :</span> <input type="text" id="prompt" /></span>`;
const promptInput = document.getElementById("prompt");
promptInput.focus();
let command = "";
promptInput.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
command = promptInput.value.trim();
writeToTerminal(`<span>${text} :</span> ${command}`);
promptInput.value = "";
const promptLine = document.getElementById("promptLine");
promptLine.remove();
inputField.focus();
resolve(command);
}
});
return new Promise((resolve) => {
terminalOutput.innerHTML += `<span id="promptLine"><span>${text} :</span> <input type="text" id="prompt" /></span>`;
const promptInput = document.getElementById("prompt");
promptInput.focus();
let command = "";
promptInput.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
command = promptInput.value.trim();
writeToTerminal(`<span>${text} :</span> ${command}`);
promptInput.value = "";
const promptLine = document.getElementById("promptLine");
promptLine.remove();
inputField.focus();
resolve(command);
}
});
});
}

//confirm
async function customConfirm(text = "") {
return new Promise((resolve) => {
terminalOutput.innerHTML += `<span id="confirmLine"><span>${text} [Y/n]</span> <input type="text" id="confirm" /></span>`;
const confirmInput = document.getElementById("confirm");
confirmInput.focus();
let command = "";
confirmInput.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
command = confirmInput.value.trim().toLowerCase();
const accepted = command === "y" || command === "";
writeToTerminal(`<span>${text} :</span> ${command}`);
confirmInput.value = "";
const confirmLine = document.getElementById("confirmLine");
confirmLine.remove();
inputField.focus();
resolve(accepted);
}
});
return new Promise((resolve) => {
terminalOutput.innerHTML += `<span id="confirmLine"><span>${text} [Y/n]</span> <input type="text" id="confirm" /></span>`;
const confirmInput = document.getElementById("confirm");
confirmInput.focus();
let command = "";
confirmInput.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
command = confirmInput.value.trim().toLowerCase();
const accepted = command === "y" || command === "";
writeToTerminal(`<span>${text} :</span> ${command}`);
confirmInput.value = "";
const confirmLine = document.getElementById("confirmLine");
confirmLine.remove();
inputField.focus();
resolve(accepted);
}
});
});
}

0 comments on commit 1b4f8d3

Please sign in to comment.