Backpack Mistake explainer


#63

hi did this is this ok


#64


Hi I did this .
Is it okay or have to some more in this.pls reply


#65

Hey @Kanvardeep_Sandhu,

You’ve got an extra line of code that’s not needed to solve this puzzle. If you have a look at @Beth_Barany’s solution see if you can spot the extra line of code in your example, then remove that.

H :slight_smile:


#66

Hey @Dark_Inc,

You’ve added an unnecessary variable. Although the code runs, it’s not the most efficient way to solve the problem.

Try instead putting otherBackpack.food inside the for loop, rather than the variable lol.

H :slight_smile:


#67

dear,
i know my code is wrong even the output seems okay. still observing to what’s going on.


#68

I solved this one after two days of messing around with it. Completely by accident too.


#69

Hey @Wihadi_Santoso_Indra,

You’ve got an extra variable that’s confusing things. otherBackpack is initially the name of the object and holds multiple properties. Then you’ve created a new variable also called otherBackpack and assigned it the specific value held by the food property. This can be confusing for other people who use your code, or if you come back to your code at some point the future.

The best practice is to name your variables uniquely to avoid potential confusion like this.

In this specific case, you don’t even need a variable! Instead, try something like for (var element of otherBackpack.food) directly inside the for loop.

Hope this helps!
H :slight_smile:


#70

I think this is critical to improving your app. As someone new to coding, I feel as though the app’s creators haven’t provided enough context for several problems. I didn’t know that the contents of backpackOther.food was already assigned, and didn’t see any clue that it was. There should definitely be some indication when a problem has hidden code. Perhaps keep the code hidden by default with a button to expand it for those of us who need to visualize. Otherwise it feels like guess work and that I’m not learning.


#71

Thank you. This explanation helped me understand the link between arrays and asking it to print an element rather than build a new array. As stated elsewhere in this thread it doesn’t state that there is hidden code except within the pretext of the story. It should be highlighted that “climbers should check the food element of the new backpack” to solidify this.

Just my two cents. But thanks for offering this explanation.


#72

hi, H
thanks so much for the replay.
the hints are useful. i’m heading to the next one.


#73

I managed to code the exercise but I still don’t understand how they know the elements in the otherBackpack if they are not written anywhere.
Can someone explain that to me please?


#74

The bottom of the original post has an “Additional Code” section that includes code that is run before your code runs. For this puzzle, the additional code is:

var otherBackpack = {
    food: ['crackers', 'chocolate', 'raisins'],
    equipment: ['map', 'rope', 'compass'],
    clothing: ['hat', 'umbrella', 'boots']
};

The reason for this is to show how you can write code to work with data that you didn’t create yourself.

–Frankie


#75

I kind of needed to know there was “Additional Code.” I frustratingly tried to write this bit (because I didn’t know it was hidden there and the instructions led me to believe I was going to write from scratch), but Grasshopper wouldn’t let me assemble it. I hate cheat-codes or walk-throughs but finally gave in… a little context goes a long way.


#76

I can’t get past the second to last test on the first group

this is all the choices I’m given


#77

It is possible to solve this puzzle with the 5 buttons pictured. Take a look at the posts above to see if you can finds some clues on how to finish this one.

–Frankie


#78

I can solved the puzzel as your instructions. Thanks for your nice explanation


#79

@Grasshopper_Frankie
Instead of using for loop can’t we directly use print function as print(otherbackpack.food) ??


#80

print(otherBackpack.food) will print out the entire array all at once. To print out the items one at a time (and complete this puzzle), you’ll need the For Loop.

If all we need to do is look at the contents, you’re correct that printing the array directly will work. There are some differences though. For example:

var myArray = [
  [1,2,3],
  [4,5,6]
];

print(myArray) will output 1,2,3,4,5,6

for (var element of myArray) {
  print(element);
}

will output:

1,2,3
4,5,6

It looks similar but it makes a big difference if instead of just printing you wanted to edit these values in some way. What if you wanted to make all of your food a “diet” version. ‘diet crackers’, ‘diet chocolate’, ‘diet raisins’ (just as a silly example).

print('diet ' + otherBackpack.food) will give you 'diet crackers,chocolate,raisins'

for (var element of otherBackpack.food) {
  print('diet ' + element)
}

will give you:

'diet crackers'
'diet chocolate'
'diet raisins'

–Frankie


#81

Ok that means if it’s required to add some extra text in the elements we have to use for loop.
Thanks for the explanation :slight_smile:️:slight_smile:️


#82

That’s right – if you want to add extra text, or delete specific items, or make any edit to an element, etc. Basically if you want to do anything more than simply printing all the values, you use the for loop to get each element one at a time instead of everything all at once :slight_smile:

–Frankie