|
var guess = { |
|
// (A) PROPERTIES |
|
// (A1) HTML ELEMENTS |
|
hform : null, // number guess form |
|
hnum : null, // number input field |
|
htries : null, // number of tries |
|
hhint : null, // hint |
|
hbtn : null, // guess/reset button |
|
|
|
// (A2) FLAGS |
|
min : 0, // min allowed number |
|
max : 0, // max allowed number |
|
jackpot : 0, // the correct number |
|
guesses : 0, // total nunmber of guesses made |
|
|
|
// (B) INITIALIZE |
|
init : () => { |
|
// (B1) GET HTML ELEMENTS |
|
guess.hform = document.getElementById("guess-wrap"); |
|
guess.hnum = document.getElementById("guess-num"); |
|
guess.htries = document.querySelector("#guess-tries i"); |
|
guess.hhint = document.getElementById("guess-hint"); |
|
guess.hbtn = document.getElementById("guess-btn"); |
|
|
|
// (B2) MIN/MAX NUMBER |
|
guess.min = Math.ceil(guess.hnum.min); |
|
guess.max = Math.floor(guess.hnum.max); |
|
|
|
// (B3) RESET GAME |
|
guess.reset(); |
|
}, |
|
|
|
// (C) RESET GAME |
|
reset : () => { |
|
// (C1) RESET FLAGS |
|
guess.guesses = 0; |
|
// credits: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random |
|
guess.jackpot = Math.floor(Math.random() * (guess.max - guess.min + 1)) + guess.min; |
|
console.log("Jackpot - " + guess.jackpot); // for the cheaters |
|
|
|
// (C2) RESET HTML |
|
guess.hnum.value = guess.min; |
|
guess.hnum.readOnly = false; |
|
guess.htries.innerHTML = 0; |
|
guess.hhint.innerHTML = ""; |
|
guess.hhint.className = ""; |
|
guess.hbtn.value = `Make a Guess (${guess.min} to ${guess.max})`; |
|
guess.hform.onsubmit = () => guess.check(); |
|
return false; |
|
}, |
|
|
|
// (D) MAKE A GUESS |
|
check : () => { |
|
// (D1) GET CHOSEN NUMBER + RESET CSS |
|
var num = +guess.hnum.value; |
|
guess.guesses++; |
|
guess.htries.innerHTML = guess.guesses; |
|
guess.hhint.classList.remove("high"); |
|
guess.hhint.classList.remove("low"); |
|
|
|
// (D2) HIT! |
|
if (num==guess.jackpot) { |
|
guess.hhint.innerHTML = "Nailed it!"; |
|
guess.hhint.classList.add("hit"); |
|
guess.hnum.readOnly = true; |
|
guess.hbtn.value = "Reset"; |
|
guess.hform.onsubmit = () => guess.reset(); |
|
} |
|
|
|
// (D3) MISS - GIVE HINTS |
|
else { |
|
// (D3-1) HIGHER OR LOWER? |
|
let difference = num - guess.jackpot, text = ""; |
|
if (difference>0) { |
|
text = "high"; |
|
guess.hhint.classList.add("high"); |
|
} else { |
|
text = "low"; |
|
guess.hhint.classList.add("low"); |
|
} |
|
|
|
// (D3-2) TOO MUCH OR CLOSE? |
|
difference = Math.abs(difference); |
|
if (difference>20) { |
|
text = "Too " + text; |
|
} else if (difference>=10) { |
|
text = "Slightly " + text; |
|
} else { |
|
text = "A little " + text; |
|
} |
|
|
|
// (D3-3) INTERFACE UPDATE |
|
guess.hhint.innerHTML = text; |
|
} |
|
return false; |
|
} |
|
}; |
|
window.addEventListener("load", guess.init); |