Question
I didnt get the helped I needed in my previous questions as prototypes were not used. I need to use prototypes. It seems that this.pGuess
I didnt get the helped I needed in my previous questions as prototypes were not used. I need to use prototypes. It seems that this.pGuess is not being set when game.prototype.PlayersGuessSubmission checks if it is a number and then calls checkGuess.
html file:__________________________
Guessing Game
Guess a number between 1-100!
Previous Guesses:
js file_______________________________________________
window.onload = function (){
startGame();
};
function makeGuess(){
var guess = parseInt(document.getElementById("num").value);
game.prototype.playersGuessSubmission(guess);
}
var game = function(){
this.pGuess = null;
this.rand = randomNumberGenerator();
this.last=[];
}
function randomNumberGenerator(){
return Math.floor((Math.random() * 100) + 1);
}
function startGame(){
return new game();
}
game.prototype.closeness = function(){
return Math.abs(pGuess-rand);
}
game.prototype.isLower = function() {
return this.pGuess
}
game.prototype.playersGuessSubmission = function(guess) {
if(typeof guess !== 'number' || guess 100) {
throw "That is an invalid guess.";
}
this.pGuess = guess;
return this.checkGuess();
}
game.prototype.checkGuess = function() {
if(this.pGuess == this.rand) {
document.getElementById("status").innerHTML = "You Win";
}
else {
if(this.last.indexOf(this.pGuess) > -1) { /* -1 if element not in array*/
return 'You have already guessed that number.';
}
else {
this.last.push(this.pGuess);
if(this.last.length === 5) {
document.getElementById("status").innerHTML = "You Lose";
}
else {
var entry = document.createElement('li');
entry.appendChild(document.createTextNode(this.guess));
unordered.appendChild(entry);
var diff = this.closeness();
if(diff
document.getElementById("status").innerHTML = "Hot";
document.getElementById("status").style.textShadow = '0 0 20px #fefcc9, 10px -10px 30px #feec85, -20px -20px 40px #ffae34, 20px -40px 50px #ec760c, -20px -60px 60px #cd4606, 0 -80px 70px #973716, 10px -90px 80px #451b0e';
document.getElementById("status").style.color = "#FFCC00";
}
else if(diff
else if(diff
else{
document.getElementById("status").innerHTML = "Ice Cold";
document.body.style.backgroundImage = "url('ice-background-14140151326az.jpg')";
}
}
}
}
}
function hint(){
game.prototype.provideHint();
}
game.prototype.provideHint = function() {
var hintArray = [this.rand, randomNumberGenerator(), randomNumberGenerator()];
return shuffle(hintArray);
}
function shuffle(arr) { //Fisher-Yates - https://bost.ocks.org/mike/shuffle/
for(var i = arr.length-1; i > 0; i--) {
var randomIndex = Math.floor(Math.random() * (i + 1));
var temp = arr[i];
arr[i] = arr[randomIndex];
arr[randomIndex] = temp;
}
document.getElementById("status").innerHTML = "Guessing Game";
return arr;
}
38 39 40 game . prototype . checkGuess 41 42 43 function() if(this .pGuess == this . rand) { document.getElementById("status").innerHTML - "You Win"; Ado else f 45 if(this.last.indexOf(this.pGuess) 1) -1 if element n Call Unable to get property 'indexof' of undefined or null reference return 'You have already guessed that number.' 46 47 48 49 50 51 Ma else this.last.push(this.pGuess) if(this . last. length = 5) { document.getElementById("status").innerHTML "YouStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started