Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialHamzah Iqbal
Full Stack JavaScript Techdegree Student 438 PointsHelp on this code, wont show any prompt.
Im doing the Quiz Challenge, but im not getting any prompt. Can i get any help on this?
Also is the coding written probably?
var = guessCount
var = questions
var = notright
[
["What is my computers name?" , "HP"],
["What is laptop color?", "brown"],
["What date is it today?", "0303"]
];
function print(message) {
document.write(message);
}
for ( var questions = 0; < questions.length; questions +=1) {
var answers = prompt (questions [i][0] );
if (answers === (questions [i] [1] ) {
guessCount ++;
else NotCorrect ++;
}
print("Correct answers" + guessCount + "not correct" + NotCorrect);
3 Answers
anthony amaro
8,686 Pointsvar = guessCount // you need to assign guessCount = 0; . since you want to update it later if the user guess the question.
var = questions // i believe this is the array..
var = notright // this can be the same as guessCount, to set the value to 0;
// var guessCount = 0;
// var questions = [];
//var notRight =0;
[
["What is my computers name?" , "HP"],
["What is laptop color?", "brown"],
["What date is it today?", "0303"]
];
function print(message) {
document.write(message);
}
for ( var questions = 0; < questions.length; questions +=1) {
// here you are trying to loop to the array that does'tn exist, i believe that is why it wont display the questions
var answers = prompt (questions [i][0] ); // you got this lines right
if (answers === (questions [i] [1] ) { // and this is right too
guessCount ++;
else NotCorrect ++; // here you just need to move this outside the if statement curly braces and add two more curly braces to the else {}
}
print("Correct answers" + guessCount + "not correct" + NotCorrect);
here is my example
var guessCount = 0;
var notRight = 0;
var questions = [
["What is my computers name?", "HP"],
["What is laptop color?", "brown"],
["What date is it today?", "0303"]
];
function print(message) {
console.log(message); // i just changed this to console.log because that's what i use
}
for (var i = 0; i < questions.length; i++) {
var answers = prompt(questions[i][0]);
if (answers === questions[i][1]) {
guessCount++;
} else {
notRight++
}
}
print('you got ' + guessCount + ' answers right');
print('you got ' + notRight + ' answers wrong');
i hope this helps
kevin hudson
Courses Plus Student 11,987 PointsHere is some tips with variables and arrays
// instantiate global variables (counters) and initialize to zero
var qCounter = 0;
var numRight = 0;
var numWrong = 0;
//create arrays for data. Some empty where you will later push to and use to list them in the HTML document while the main one is a dimentional array with questions and ansers [i][0]
var rightAnswer = [];
var wrongAnswer = [];
var questionPool = [
['158=___ + 106. What number will come in the blank to make the number sentence true?', '52'],
['Which moon is larger than Mercury?', 'io'],
['What is the name of the little dragon in the animated movie Mulan?', 'mushu'],
['What is the name of the prison in the film The Rock?', 'alcatraz'],
['Give another name for the study of fossils?', 'paleontology'],
['Which insects cannot fly, but can jump higher than 30 cm?', 'flea'],
['Which plant does the Canadian flag contain?', 'maple'],
['Which is the largest desert on earth?', 'sahara'],
['What is the largest state of the United States?', 'alaska'],
['What is the capital of the American state Hawaii?', 'honolulu'],
];
inside loop you will push answers to empty arrays
//main program. Iterate through array. Bring answer to lower case. Count right/wrong.Push to appropriate arrays
for (var i = 0; i < questionPool.length; i++) {
var question = questionPool[i][0];
var answer = questionPool[i][1];
var askQuestion = prompt(question.toLowerCase());
if (answer === askQuestion) {
numRight++;
rightAnswer.push(question);
} else {
numWrong++;
wrongAnswer.push(question);
}
}
anthony amaro
8,686 Pointsyeah but when you create a variable just remember to use the var keyword, then the name of the variable then the value you want to assign it to
var guessCounter = 0;
var questions = [];
just keep on practicing :)
Hamzah Iqbal
Full Stack JavaScript Techdegree Student 438 PointsHamzah Iqbal
Full Stack JavaScript Techdegree Student 438 PointsThank you so much! Makes so much sense! So basically, the reason why the array didnt exist is that i called on top once, and never declared/called it later right?