[Solved] Mountain -> slalom cant understand instructions

Hi I cant quite understand the object usage and (there no code of mine here, only insturctions of the level):

// Use object literals to walk the safe path and collect the gems.
// You cannot use moveXY() on this level! Use move() to get around.
var gems = hero.findItems();

why here its using "hero.move({‘x’: 20, ‘y’: 35});"

while (hero.pos.x < 20) {
	// move() takes objects with x and y properties, not just numbers.
	hero.move({'x': 20, 'y': 35});
}

but here its using "hero.move(gem0.pos);"

while (hero.pos.x < 25) {
	// A gem's position is an object with x and y properties.
	var gem0 = gems[0];
	hero.move(gem0.pos);
}

and I dont understand what it wants me to do in the two following - gem[i]? object literal{}?

// While your x is less than 30,
// Use an object to move to 30, 35.

// While your x is less than 35,
// Move to the position of gems[1].

// Get to the last couple of gems yourself!

ty

Have u try reading through the hints?

1 Like

Yes, still dont understand

An object literal in this case is a python dictionary. That is something inside curly brackets {} which has a string value first e.g. “x” which is called a key, then another value after a colon, which can be any data type (integer, float, string etc.).
An example of a dictionary is:

cars = {"type": "Ford", "speed": 150, "acceleration": 1.5, "release-date": "1997"}

I’ve used a string, and integer and a float as data values in my cars dictionary.
If you lay it out like this it’s easier to see:

cars = {
    "type": "Ford", 
    "speed": 150, 
    "acceleration": 1.5, 
    "release-date": "1997"
}

To use those values you have to do this:

speed = cars["speed"]

That will access the value behind the “speed” key. So the speed variable will be 150.
We don’t use that that much in CoCo, but I wanted to explain it to you anyway. Our dictionary is:

pos = {
    "x": 30
    "y": 35
}

Code combat’s move() function has been programmed to accept this type of input. So:

hero.move(pos) # this would work using the above pos variable.

You can make a dictionary like mine and use it inside the brackets of the move() function. This is telling the hero where to go, what x and y to move to.
Gems[i].pos is also an object literal. It will have the same values as our set, so you can do gems[i].pos.x, because pos has an x value.
I hope this helps.
Danny

4 Likes

ty for the in deep explanation :smile:
I didnt quite understand the part of the gems[i], If im telling the hero to move somewhere using move({‘x’: 20, ‘y’: 20}), why do I also have the move(gem[i].pos)?

That’s because the bottom gem’s position is always you next destination after you get to any of the top points. So you could use another object literal like {“y”: 15, “x”: 20}, or you could use gems[i].pos which holds the same value.
Danny

3 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.