-
Notifications
You must be signed in to change notification settings - Fork 0
/
guess-number
91 lines (77 loc) · 3.03 KB
/
guess-number
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
function Game(){ //constructor function
this.game={};
this.playersGuess;
this.pastGuesses=[];
this.winningNumber = function(){ return generateWinningNumber();};
}
Game.prototype.playersGuessSubmission = function playersGuessSubmission(this.playersGuess){ //validation for this.playersGuess
if(typeof this.playersGuess !== "number"){
alert("That is an invalid this.playersGuess."); //this.playersGuess type cannot be NaN and not a number
}else if(this.playersGuess < 1 || this.playersGuess > 100){
alert("That is an invalid this.playersGuess. Please this.playersGuess a number between 1 and 100.") //has to be a this.playersGuess between 1 and 100 (inclusive)
}else if(this.checkGuess.length < 5) //stores this.playersGuess up to 5
this.pastGuesses.push[this.playersGuess];
}else{
checkGuess(this.playersGuess); //calls the checkGuess function
}
};
Game.prototype.checkGuess = function checkGuess(this.playersGuess, this.winningNumber){
if(pastGuesses.length === 5){ //checks the this.playersGuess of past guesses
return "You lose.";
}else if(pastGuesses.indexOf(this.playersGuess)){ //checks for duplicates and alerts
this.pastGuesses.push(this.playersGuess);
return "You have already guessed that this.playersGuess.";
}else if(this.difference === 0){ //if this.playersGuess is exact match
return "You Win!";
}else{ //else alert message by how far off the this.playersGuess is off
if(this.difference <= 10){
return "You're burning up!";
}else if(this.difference <= 25 && this.difference > 10){
return "You're lukewarm.";
}else if(this.difference <= 50 && this.difference > 25){
return "You're a bit chilly.";
}else if(this.difference <= 100 && this.difference > 50){
return "You're ice cold.";
}else{
return "Something is off, try again.";
}
}
};
Game.prototype.difference = function difference(isLower(this.playersGuess,this.winningNumber)){
if(this.isLower){
return this.winningNumber-this.playersGuess;
}else{
return this.playersGuess-this.winningNumber;
}
};
Game.prototype.isLower = function isLower(this.playersGuess, this.winningNumber){
return this.playersGuess < this.winningNumber;
};
Game.prototype.provideHint = function provideHint(){
};
function generateWinningNumber(){
var rb = ((Math.random()*100)+1).toString(); //inclusive of 100
if (retNum(rb[rb.indexOf('.')-1]) % 2 === 0){
return Math.floor(retNum(rb.slice(0,rb.indexOf('.')))); //rounddown if even
}else{
return Math.ceiling(retNum(rb.slice(0,rb.indexOf('.')))); //roundup if odd
}
}
function retNum(str){
return Number(str);
}
function newGame(Game){
var newGame = Object.create(Game.prototype);
newGame.__proto__=Game.prototype;
return newGame;
}
function shuffle(array){
var l = array.length;
while(l){
var t= Math.floor(Math.random()*l -= 1),q; //scoped within while
array[t]=q; //random value in the array
array[q]=array[l]; //
array[l]=array[t];
}
return array;
}