0

I have an object like so:

answers = {
   "q1" :  {
        "selected": "a",
        "correct": "b"
        },
   "q2": {
        "selected": "c",
        "correct": "d"
       },
    etc...
}

how do I compare selected and correct?

i'm trying

for (let key in answers) {
    if (key.selected === key.correct) {
        correct++;
    } else {
        incorrect++;
    }
}

but it doesn't work. I keep getting all questions marked correct. Pretty sure I need to do something else to go down a level in the object but not sure how to do it.

if I do answers[key].selected === answers[key].correct all come out as incorrect

isherwood
  • 58,414
  • 16
  • 114
  • 157
Vidro3
  • 192
  • 2
  • 11
  • 1
    Like `answers[key].selected` and `answers[key].correct` maybe? – Alon Eitan May 15 '17 at 20:38
  • `key` is a string of the current key. Please review how `for-in` works: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in –  May 15 '17 at 20:38
  • Possible duplicate of [Iterating over objects in Javascript](http://stackoverflow.com/questions/8389148/iterating-over-objects-in-javascript) – Alon Eitan May 15 '17 at 20:43

3 Answers3

1

Well... you're comparing the keys and not the values, where selected and correct will be undefined for both cases thus correct. You probably meant:

if (answers[key].selected === answers[key].correct) {
Joseph
  • 117,725
  • 30
  • 181
  • 234
  • 1
    Should we answer this or close it as duplicate? There are probably at least 10 questions about `for( key in object )` – Alon Eitan May 15 '17 at 20:41
  • this results in all my answers coming up as incorrect even though their values match in the answers object. any ideas if I have anything else wrong? – Vidro3 May 15 '17 at 20:44
  • @Vidro3: WRT `answers[key].selected === answers[key].correct` Look at your data. In none of the objects is the `.selected` property the same as the `.correct` property. We don't know what you actually need because you haven't posted a complete yet minimal demo with a description of what the behavior should be beyond what you've shown. –  May 15 '17 at 20:51
  • 1
    @squint ahh crud you are correct. some of my values are strings and others are int. Have to see how that happened. Thanks – Vidro3 May 15 '17 at 20:55
0

You want:

for (let key in answers) {
  if (answers[key]["selected"] === answers[key]["correct"]) {
    correct++; 
  } else {
    incorrect++; 
  }
}
  • 1
    `answers.key.selected` won't give the desired property, and you removed the variable `let` declaration for some reason. –  May 15 '17 at 20:42
0

you need to use it like this

answers = {
   "q1" :  {
        "selected": "a",
        "correct": "b"
        },
   "q2": {
        "selected": "c",
        "correct": "c"
       }

};
var correct=0;incorrect=0;
for (let key in answers) {
 
 if (answers[key].selected === answers[key].correct)
 {
   correct++;
 }
else
{
   incorrect++;
}
}
   console.log(correct);
   console.log(incorrect);
JYoThI
  • 11,977
  • 1
  • 11
  • 26