Tell us what’s happening:
I tried to write a for loop that iterates through all items in obj and adds to a counter variable if an item is in the list of names. If all items are in the list of names, the counter should be equal to the length of the object. If the counter == obj.length, return true. Otherwise, return false.
My main problem is that I have no idea how to assess what’s working and what’s not working in this code because I’m not getting any output. I even put in “console.log(x);” to see what this returns, but nothing is showing up in my console.
Could somebody please walk me through how to approach this? Thanks!
Your code so far
let users = {
Alan: {
age: 27,
online: true
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: true
},
Ryan: {
age: 19,
online: true
}
};
function isEveryoneHere(obj) {
// change code below this line
const req = ['Alan', 'Jeff','Sarah','Ryan'];
let x = 0;
for (i=0; i<obj.length; i++){
if (obj[i] in req){
x++;
else {
break;
}
}
}
console.log(x);
if (x == obj.length){
return true; } else {
return false;
}
// change code above this line
}
console.log(isEveryoneHere(users));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.
You’ve got the right idea, but there’s a couple of mistakes that means this is malformed javascript and simply won’t run at all, it just crashes
i is not defined, you should use let i=0 in the loop
your else statement is somehow inside the if, which isn’t valid
Your indentation is very awkward, which is probably why you didn’t see the second issue, consider writing if and else statements in the following way:
if (thing) {
doThing();
}
else {
doOtherThing();
}
so you can more easily see the indentation and find these sorts of bugs. That final if/else in particular is exceptionally hard to read
Edit: Once you’ve fixed it being malformed javascript, there’s still an issue that means you won’t pass the tests, but your console.log(x) should help you diagnose it - good luck!
I truly tried to dig into this. First I did "console.log(x) and got “0”.
Then I tried “console.log(obj[i])” and got “i is undefined”.
Then I tried “console.log(obj[0])” and I also got undefined.
Then I tried "console.log(users[0]) and I ALSO got undefined.
I don’t know what to do now. I hate feeling like I’m asking for the answer instead of putting in the effort to figure out what’s not working but I have no idea WTF to do…
You were very close with your solution, counting how many people are there.
I’ll explain how I went about figuring it out, incase something in this explanation helps you debug similar things in the future, and then I’ll say what the correction is
obj[i] was undefined
This suggests that something about obj isn’t what we were expecting. let’s console.log(obj) the second we enter the function
…and when we do that, we see that obj is actually an object and not an array!
So indexing with an integer i doesn’t work like we expected it to!
But we do have an array we can do this over, let’s change it round: