 Welcome to challenge 15! Use this thread for any and all questions relating to challenge 15.

I’m going to work on a solution using `extends` that builds-in an array method in place of `countElems` below. I didn’t do that here.

``````// a function that counts how many 'target' appear in 'arr'
const countElems = (arr, target) => {
let sum = 0;
for (let elem of arr) {
if (elem === target) {
sum++;
}
}
return sum;
}

// since we only need to know the end position, just subtract moves west from east; similarly for north and south
const finalPosition = (moves) => {
let x = countElems(moves, "east") - countElems(moves, "west");
let y = countElems(moves, "north") - countElems(moves, "south");
return [x, y];
}``````
```const finalPosition = (moves) => {
const movement = [0, 0]; // x, y

for (let i = 0; i < moves.length; i++) {
if (moves[i] === 'north') {
movement++;
} else if (moves[i] === 'south') {
movement--;
} else if (moves[i] === 'east') {
movement++;
} else if (moves[i] === 'west') {
movement--;
}
}
return movement;
}
```
6 Likes

Hi,
This challenge is difficult for a beginner like me because there are not many resources on multidimensional arrays and I don’t know how to proceed.
This is my code that I’m starting:
const finalPosition = (moves) => {
// Code here!

``````  arrayMoves = [0 , 0];
for(var i = 0; i < arrayMoves.length; i++){
for(var j = 0; j < parade.length; j++){

}

}

};``````

You don’t need a multidimensional array, and you certainly don’t need nested `for` loops. Just keep track of `x` and `y` separately.

And here it is… kinda. The issue I’m having is that rather than just extending the array, the way the constructor works is that `ExtArr` is actually creating an array of the original `Array`, so I get [[`values`]]. I get the sense that `constructor()` is one of those “behind the scenes” kind of things, because I can’t figure out how it actually works. Changing to `constructor() { super() }` is unhelpful. Solutions?

``````class ExtArr extends Array {

constructor(...args) {
super(...args);
}

countElems (target) {
let sum = 0;
for (let elem of this) {
console.log(elem);
if (elem === target) {
sum++;
}
}
return sum;
}
}

const finalPosition = (moves) => {
// Code here!
let movesToo = new ExtArr(moves);
let x = movesToo.countElems("east") - movesToo.countElems("west");
let y = movesToo.countElems("north") - movesToo.countElems("south");
return [x, y]
}``````

The name of the function in the instructions paradePosition() doesn’t match the name finalPosition in the code and the test cases, it may cause misunderstanding

2 Likes

Can anyone tell me if this code is correct?

``````const finalPosition = (moves) => {
// Code here!

arrayMoves = [0 , 0];
for(var i = 0; i < arrayMoves.length; i++){

var parade = arrayMoves[i] + arrayMoves[i];
if (moves ==='north') {

}else if (moves ==='west') {

}else if(moves ==='east'){
}else if(moves ==='south'){
}

}

};
``````

Because I don’t understand if my logic up to this point is correct.

Please i need a proper explanation of what this challenge is all about!
i am confused at this point…
thanks for any quick response

Hi. So the challenge is asking to return coordonates of a graph [x,y].
The x coordinate will increase by 1 for every 'east ’ element in the input array.
The x coordinate will decrease by 1 for every ‘west’ element in the input array.

The y coord will increase by 1 for every ‘north’ element in the input array.
The y coord will decrease by 1 for every ‘south’ element in the input array.

Hope it helps.

3 Likes

the main issue here is that your `parade` array will be overwritten each time the loop starts over. Having `arrayMoves` above the loop is the right idea, but you need to declare it with var. Hopefully that gives you a bit of a hint.

I change but I have errors yet.

``````const finalPosition = (moves) => {
// Code here!

var  arrayMoves = [0 , 0];
for(var i = 0; i < arrayMoves.length; i++){

if (moves =='north') {
arrayMoves +=1;

}else if (moves =='west') {
arrayMoves -=1 ;

}else if(moves =='east'){
arrayMoves +=1;
}else if(moves =='south'){
arrayMoves -=1;
}
return arrayMoves;

}

};``````
1 Like

My weird 4 AM solution. My first approach used a switch, then an if…else if, then back to a switch…I was losing control of the situation!!! So, I instead went for a solution I thought might be interesting and that made me think.

Summary
``````const finalPosition = (moves) => {
const coords = moves.reduce((allMoves, move) => {
allMoves[move]++;
return allMoves;
}, { east: 0, west: 0, north: 0, south: 0 });
return [coords['east'] - coords['west'], coords['north'] - coords['south']];
}
``````
1 Like

You’re almost there!! Take a look where your return statement is. Is that where you want it?

with a reducer

``````const finalPosition = (moves) => moves.reduce(
([x,y],next)=> {
switch(next){
case "north": return [x, y+1];
case "south": return [x, y-1];
case "east": return [x+1, y];
case "west": return [x-1, y];
default: return [x,y];
}
},
[0,0]
)
``````
5 Likes

thank you so much @HinaJ-R; your explanation just gave me a different view and understanding

1 Like
1. You need to specify which element you are in the moves array. Right now, you are comparing the entire array to ‘north’, ‘south’…
2. Your return should not be in the loop.
3. Your loop will run for only two iterations. Are you using the proper [array].length in the for loop?
1 Like

When loop ends i want that return my array