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 trial

JavaScript

Matias Verardo
Matias Verardo
4,371 Points

Can somebody please help me? im going nuts!!!!!!

Hi everybody, im having a trouble with this piece of code.. Im trying to make a "quizz app" by myself, the problem is the first if. please help me

var preguntasYrespuestas = [ {pregunta:'1+1', respuesta:'2'}, {pregunta:'2+2', respuesta:'4'}, {pregunta:'3+3', respuesta:'6'}, {pregunta:'4+4', respuesta:'8'}, {pregunta:'5+5', respuesta:'10'} ];

var pregunta ; var respuesta; var respUser; var respCorr = 0; var respInc = 0; var pregCorr = []; var pregInc = [];

for(var i = 0; i < preguntasYrespuestas.length ; i += 1) { pregunta = preguntasYrespuestas[i].pregunta; respuesta = preguntasYrespuestas[i].respuesta; respUser = parseInt(prompt(pregunta));

if (respUser === preguntasYrespuestas [i][1]) {
    pregCorr.push(pregunta);
    respCorr += 1;
    console.log('se agrego una resp correcta');
}
else {
    pregInc.push(pregunta);
    respInc += 1;
    console.log('se agrego una res incorrecta');
}

} console.log('Tuviste un total de ' + respCorr + ' correctas, ellas fueron ' + pregCorr +'.'); console.log('Tuviste un total de ' + respInc + ' incorrectas, ellas fueron ' + pregInc +'.'); console.log(respCorr); console.log(respInc); console.log(pregCorr); console.log(pregInc);

The last four console.log() are for test, do not include that. please helpme !

2 Answers

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

I made a few tweaks to your code:

var preguntasYrespuestas = [
    {pregunta:'1+1', 
    respuesta:'2'}, 
    {pregunta:'2+2', 
    respuesta:'4'}, 
    {pregunta:'3+3', 
    respuesta:'6'}, 
    {pregunta:'4+4', 
    respuesta:'8'}, 
    {pregunta:'5+5', 
    respuesta:'10'} 
];

var pregunta; 
var respuesta; 
var respUser; 
var respCorr = 0; 
var respInc = 0; 
var pregCorr = []; 
var pregInc = [];

for(var i = 0; i < preguntasYrespuestas.length; i++) {
    pregunta = preguntasYrespuestas[i].pregunta; 
    respuesta = preguntasYrespuestas[i].respuesta; 
    respUser = prompt(pregunta);

    if (respUser === respuesta) {
        pregCorr.push(pregunta);
        respCorr += 1;
        console.log('se agrego una resp correcta');
    }
    else {
        pregInc.push(pregunta);
        respInc += 1;
        console.log('se agrego una res incorrecta');
    }
}

console.log('Tuviste un total de ' + respCorr + ' correctas, ellas fueron ' + pregCorr +'.'); 
console.log('Tuviste un total de ' + respInc + ' incorrectas, ellas fueron ' + pregInc +'.'); 
console.log(respCorr); console.log(respInc); console.log(pregCorr); console.log(pregInc);

(these are my line numbers in my refactored version, not the same as your original...)

  • line 22 I complete the loop here. My suspcicion is that you did this right, but it's not showing up because the treehouse forum has some bugs displaying text, but all this can be made better if you format your code as they explain in this article when posting to the forum: https://teamtreehouse.com/community/posting-code-to-the-forum
  • line 26 I took out the ParseInt. The user's answer is a string, and your data is a string, so we shouldn't convert to Int it's not necessary and we don't want the comparison to fail because of type
  • line 28 there were a few things wrong with the code that you had ((respUser === preguntasYrespuestas [i][1]). 1) Make sure that there's no space between the variable and the index brackets [], 2) the next set of index brackets you shouldn't use because this item is a dictionary not a list. But most importantly 3) all of this isn't necessary because you already put this answer into a variable on line 24 (respuesta).
Matias Verardo
Matias Verardo
4,371 Points

thanks for taking time to answer me, it was very helpful ! really thanks !

Matias Verardo
Matias Verardo
4,371 Points

thanks for taking time to answer me, it was very helpful ! really thanks !

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Well there's a couple of things. First, your for loop is set up incorrectly. You start with i is equal to 0 but nowhere do you ever increment i. Secondly, the thing that follows that should be an evaluation that will tell how many times the loop will execute. For examply while i is less than the length of preguntasYrespuestas. Third, you need to increment the i variable.

I suggest you start by reviewing this video here at treehouse. https://teamtreehouse.com/library/javascript-loops-arrays-and-objects/simplify-repetitive-tasks-with-loops/for-loops

Matias Verardo
Matias Verardo
4,371 Points

the problem was "if (respUser === preguntasYrespuestas [i][1]) ", i dont need to loop in the array bc i already saved the pregunta in var respuesta, that error in the for loop that you say, its a copy/paste trouble i dont know why it doest copy. thanks for answer me ! sorry for my english