-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
163 lines (139 loc) · 5.39 KB
/
app.js
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
'use strict';
var answerCorrect = 0;
var totalQuestion = 7;
//Greetings.
alert('Welcome to my page!');
//Asking user for name.
var username = prompt('What is your name?');
//Greet user with name. Asking to play a game.
alert('Hi, ' + username + '. Let\'s play a guessing game. Please answer question 1 through 5 in the form of yes/y or no/n answer.')
//Question 1
function questionOne () {
var response1 = prompt('Does Santa live in the North Pole?').toLowerCase();
if(response1 === 'yes' || response1 === 'y'){
alert('Correct! You\'re a star.');
console.log('User answered question correctly');
answerCorrect +=1;
} else if(response1 === 'no' || response1 === 'n'){
alert('Boo! You got it wrong. Santa lives in the North Pole.');
} else{
alert('Try to answer with a yes/y or no/n next time.');
}
}
questionOne ();
//Question 2
function questionTwo () {
var response2 = prompt('Is Santa married?').toLowerCase();
if(response2 === 'yes' || response2 === 'y'){
alert('Horray! You got it right!');
console.log('User answered question correctly.');
answerCorrect += 1;
} else if(response2 === 'no' || response2 === 'n'){
alert('Wrong! Santa is married.');
}else{
alert('Try to answer with a yes/y or no/n next time.');
}
}
questionTwo();
//Question 3
function questionThree () {
var response3 = prompt('Does Santa drive?').toLowerCase();
if (response3 === 'yes' || response3 === 'y'){
alert('Woohoo! Santa drives the reindeer sleight.');
console.log('User answered questions correctly.');
answerCorrect += 1;
}else if(response3 === 'no' || response3 === 'n'){
alert('Sad face..You didn\'t get this right this time.');
}else{
alert('Try to answer with a yes/y or no/n next time.');
}
}
questionThree();
//Question 4
function questionFour() {
var response4 = prompt('Does Santa like the Grinch?').toLowerCase();
if(response4 === 'yes' || response4 === 'y'){
alert('No! Santa doesn\'t like the Grinch because the Grinch stole Xmas.');
console.log('User did not answered question correctly.');
}else if(response4 === 'no' || response4 === 'n'){
alert('You\'re correct. Santa doesn\'t like the Grinch.');
answerCorrect += 1;
}else{
alert('Try to answer with a yes/y or no/n next time.');
}
}
questionFour();
//Question 5
function questionFive () {
var response5 = prompt('Is it true that Santa only give presents to good little boys and good little girls?').toLowerCase();
if(response5 === 'yes' || response5 === 'y'){
alert('Woohoo! You must have been good this year.');
console.log('User answered question correctly.');
answerCorrect += 1;
}else if(response5 === 'no' || response5 === 'n'){
alert('Wrong! Looks like somebody is getting a bag a coal this Xmas ;)');
}else{
alert('Try to answer with a yes/y or no/n next time');
}
}
questionFive();
//Question 6. This takes in numeric input. User to guess a number. Alert "too high" or "too low". Limit to 4 tries.
var numReindeer = 0;
var numAttempt = 4;
function questionSix () {
while (numReindeer !== 9 && numAttempt > 0) {
numReindeer = parseInt(prompt('How many reindeers does Santa have?'));
//set number of attempts so that they are decreased by 1 each time an attempt is taken.
numAttempt -= 1;
if(!isNaN(numReindeer)) {
// if the number of attempt is less than 4 times, then proceed with validating the answer.
if(numAttempt > 0){
// if the user input in more than 9, tell them it's too high and guess again.
if(numReindeer > 9) {
alert('Your guess is too high');
// if the user input is less than 9, tell them it's too low and guess again.
}else if(numReindeer < 9) {
alert('Your guess is too low');
// if the user input is 10, tell them it's correct.
}else{
alert('Wow!!! You\'re so smart. There are 9 reindeers');
answerCorrect += 1;
}
// if the number of attempts are more than 4 times, then alert user that no more attempts is allowed. End code.
}else{
alert('Sorry. Nice try but you ran out of attempts. Let\'s go look up reindeers.');
}
}
}
}
questionSix();
//Question 7. Add an Array for possible answers. User has up to 6 tries or until get correct answer. Alert ('correct') or ('you run out of attempts')
var choice = true;
var nameReindeer = ['dasher','dancer','prancer','vixen','comet','cupid','dunder','blixen','rudolph'];
var counter = 0;
var totalAttempts = 6;
function questionSeven () {
while (counter <6 && choice) {
var userInput = prompt('What\'s the name of one of Santa\'s reindeers?').toLowerCase();
for (var i = 0; i < nameReindeer.length; i++) {
if (userInput === nameReindeer[i]) {
alert('You are correct. Santa\'s reindeers are: Dasher, Dancer, Prance, Vixen, Comet, Cupid, Duner, Blixen and Rudolph');
counter += 1;
choice = false;
answerCorrect += 1;
break;
}
}
if(userInput !== nameReindeer[i]) {
alert('You are wrong. You have ' + totalAttempts + ' attempts left.');
counter += 1;
totalAttempts -=1;
}
}
if (counter === 6){
alert('Thanks for playing. You have run out of attempts.');
}
}
questionSeven();
//Step 5: Tally up the # of correct answers. Display message to compare it to the # of correct out of total questions.
alert('You have answered ' + answerCorrect + ' correct out of ' + totalQuestion + ' questions.');