Few questions... [JavaScript]

Alright bear with me because this is EXTREMELY long on me trying to figure stuff out but in conclusion I came up with a few questions!

/*
while(true) {
    var friend = hero.findNearest(hero.findFriends());
    hero.moveXY(friend.pos.x, friend.pos.y -6);
}
*/  

/*while (true) {
var names = ["Tharin", "Hattori"];
hero.moveXY(names[0].pos.x, names[1].pos.y);
}*/

/*while (true) {
    var friends = hero.findFriends();
    var xi = null;
    var yi = null;
    var i = 0;
    var friend = friends[i];
    if (friend && friend.pos.x == hero.pos.x) {
        xi = friend.pos.x;
    }
    if (friend && friend.pos.y == hero.pos.y) {
        yi = friend.pos.y; 
}

hero.moveXY(xi, yi);
}
*/

/*var friends = hero.findFriends();
var i = 0;
var friend = friends[i];

while (i < friends.length){
    friend = friends[i];
    hero.say(friend);
    i++;
}
*/
/* [0]"Amara", [1]"Senick", [2]"Pender", [3]"Kleene", [4]"Tharin", [5]"Omarn", [6]"Hattori", [7]"Anya", [8]"Hushbaum", [9]"Naria" (possibly try moving in sync with these)
*/

/*
var friends = hero.findFriends();
var i = 0;
var friend = friends[i];

function iy() {
while (i < friends.length) {
    friend = friends[i];
    if (friend.pos.x == hero.pos.x) {
        return friend.pos.x;
    } else {
    i++;
    }
}
}

i = 0;
function ix() {
while (i < friends.length) {
    friend = friends[i];
    if (friend.pos.y == hero.pos.y) {
        return friend.pos.y;
    } else {
    i++;
    }
}
}
while (true){
var fx = ix();
var fy = iy();
hero.moveXY(fx, fy);
}
*/


/* [0]"Amara", [1]"Senick", [2]"Pender", [3]"Kleene", [4]"Tharin", [5]"Omarn", [6]"Hattori", [7]"Anya", [8]"Hushbaum", [9]"Naria" (possibly try moving in sync with these)
*/

/*
while (true) {
var friends = hero.findFriends();
var i = 0;

hero.moveXY(friends[4].pos.x, friends[6].pos.y);
}
*/
/* 
    var friend = hero.findNearest(hero.findFriends());
    var offsetX = friend.pos.x - hero.pos.x;
    var offsetY = friend.pos.y - hero.pos.y;
while(true) {

    hero.move({'x': friend.pos.x - offsetX, 'y': friend.pos.y - offsetY});
}
*/
var friend = hero.findNearest(hero.findFriends());
var yi = friend.pos.y - hero.pos.y;
while(true) {
   // hero.move({'x':friend.pos.x, 'y': friend.pos.y - yi});
   hero.moveXY(friend.pos.x, friend.pos.y - yi);
}

  1. Alright so first of all I realize I wasn’t accounting for the offset and that’s why many of my attempts failed. in previous levels I noticed arrays set by names, so for example
/*while (true) {
var names = ["Tharin", "Hattori"];
hero.moveXY(names[0].pos.x, names[1].pos.y);
}*/

May I ask what I did wrong in defining Tharin and Hattori in their own variables? Is it because I didn’t find out if they exist? or didn’t use a find function to declare Tharin and Hattori?

  1. Next is there any easier way to figure out why exactly each number is in a array short of doing this:
/*var friends = hero.findFriends();
var i = 0;
var friend = friends[i];

while (i < friends.length){
    friend = friends[i];
    hero.say(friend);
    i++;
}
*/
/* [0]"Amara", [1]"Senick", [2]"Pender", [3]"Kleene", [4]"Tharin", [5]"Omarn", [6]"Hattori", [7]"Anya", [8]"Hushbaum", [9]"Naria" (possibly try moving in sync with these)
*/

and finding out what each number would correlate to name wise?

  1. How do I get a function to return a number?
function iy() {
while (i < friends.length) {
    friend = friends[i];
    if (friend.pos.x == hero.pos.x) {
        return friend.pos.x;
    } else {
    i++;
    }
}
}

later on I set function iy() to a variable but it wasn’t returning a number… I’ve looked back on some of my previous code examples but couldn’t find a point of reference (maybe I overlooked it)

  1. I’ve seen before code listed as spoilers where you have to click to expand it, how do I go about doing that?

That about sums up my questions, thank you in advance!

#1
The array names is assigned to some string or text,the function hero.moveXY need x, y coordinates
If you say “Tharin”.pos.x you are asking to get the x coordinates of some text, there is no coordinates for that. You need to get the object Tharin and Hattori first. And for that you’ll have no choice to do manipulations in friends
It won’t work with hero.move() either because hero.move need also an object

#2
I don’t really understand what you are asking, but if you aim for a particular character you can always ask in your loop if friend.id == “name”

#3
the problem here is that you are using a lot of variables that is not defined inside the function you’ll need to set those variables as arguments in your function.
For example, you are using the array friends and the variable i. But the function don’t know what is friends neither what is i
So you need to put arguments inside the function:
iy(a, b)
while(a < b.length) etc.
(a, b) is an argument. IMPORTANT: an argument has no value. You give them value only when you call the function.
When you call the function in your code write something like that:
iy(friends, i)

this is a spoiler

Use the tags spoiler and /spoiler (in boxes [spoil][/spoil])

1 Like

Thank you Gabriel! I’ll give your answers a go when I get a chance =) I’m finally in the Cloudrip Mountains! 13/109 so far (I just realized you probably knew this because of the code I am inquiring about lol)

boss star is awesome

1 Like