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 trialSeb Gam
2,347 PointsIs my solution ok?
var questions = [["What is 1+1?", "What is 2+3?", "What is 9-2?", "What is 5-1?"],
[2, 5, 7, 4]
];
var n = 0;
var correctAnsw = [];
var failedAnsw = [];
while(n < 4){
var allQuestions = prompt(questions[0][n]);
if (allQuestions == questions[1][n]){
alert("Correct, the number is: " + questions[1][n]);
correctAnsw.push(questions[0][n]);
}else{
alert("No, the answer is: " + questions[1][n]);
failedAnsw.push(questions[0][n]);
}
n += 1;
}
document.write("<br>" + "The correct answers are: " + "<br>" + correctAnsw.join(", " + "<br>"));
document.write("<br>" + "The incorrect answers are: " + "<br>" + failedAnsw.join(", " + "<br>"));
3 Answers
Osaro Igbinovia
Full Stack JavaScript Techdegree Student 15,928 PointsHi Seb, most of your code is good. Just two little errors, which are:
In the conditional statement in your 'while' loop when you used the 'push' method to add the correct and incorrect answers in their respective array literals. You were pushing in 'questions[0][n]' (which is the array of questions)
if (allQuestions == questions[1][n]){
alert("Correct, the number is: " + questions[1][n]);
correctAnsw.push(questions[0][n]);
}else{
alert("No, the answer is: " + questions[1][n]);
failedAnsw.push(questions[0][n]);
}
when it should be 'questions[1][n]' (which is the array of answers) just as you used in your alert dialog on the line just above.
if (allQuestions == questions[1][n]){
alert("Correct, the number is: " + questions[1][n]);
correctAnsw.push(questions[1][n]);
}else{
alert("No, the answer is: " + questions[1][n]);
failedAnsw.push(questions[1][n]);
}
Seb Gam
2,347 PointsIt should have been more along the lines: "Questions you have answered correctly." English is not my first language. Thank you very much :)
Osaro Igbinovia
Full Stack JavaScript Techdegree Student 15,928 PointsLol oh okay, since it is 'Questions you have answered correctly' you want to write to the 'document' object then leave your code the way it is concerning my initial answer, but change your concatenation in the document.write();
document.write("<br>" + "The questions you have answered correctly are: " + "<br>" + correctAnsw.join(", " + "<br>"));
document.write("<br>" + "The questions you have answered wrongly are: " + "<br>" + failedAnsw.join(", " + "<br>"));
Seb Gam
2,347 PointsThank you for your answers, fixed it and I am glad, that my code is ok :) Have a great day