Code playground


#1

I’m trying to figure out what I’m doing wrong, why does this code only print out “woo!”


#2

Hello,
So you have an array of backpacking items but are you not looping through those items. Hence it will always print woo!. Also you are comparing the object holder not the array items hence will not be equal to exaustion.

put a for loop for the backpack and then in the loop put the if statement. I hope that helps.

Example:

var backpacking = [
‘tree hammock’,
‘exaustion’,
‘fun!’

];

for(var element of backpacking)
{
if(element === ‘exaustion’)
{
print(‘bummer!’)
}
else {
print(‘woot!’)
}
}


#3

Hey @Nate_McCarthy,

In addition to @Ahsen’s solution, you can also use Array Indexing to reference a specific value from your array within your if-else statement. For example:

if (backpacking[1] === 'exaustion') {
    print('bummer');
}  else {
   print('woo!')
}

H :slight_smile: