Won't You Be My Neighbor? explainer

Aim of the puzzle: Use indexing to access the item neighboring another item in an array.
Walk through of solution:
The getNeighbor() function will take an array and an index, and return the item in the array that is next to the index.

To start, add 2 parameters to the function. Name them something like arr and i so they don’t get confused with the array and index variables that are imported into the puzzle.

Inside the function, create a variable to store the neighboring index. Give it a value of index + 1

Next, return the neighboring item from the function.

Finally, inside the console.log() at the bottom of the code, add array and index as arguments to the function.
Sample code solution:

import { array, index } from 'grasshopper.arrayFactory';

function getNeighbor (arr, i){
    let neighbor = i + 1
    return arr[neighbor]
}

console.log('The neighbor to ' + array[index] + ' is ' + getNeighbor(array,index))

Javascript Concepts: Arrays, Indexing, .length, Functions, Local Variables, Return Statement
Additional Code (hidden code that runs before the puzzle’s code):

const list = [
  ['first_item', 'second_item','third_item', 'fourth_item', 'fifth_item', 'sixth_item', 'seventh_item', 'eighth_item', 'ninth_item', 'tenth_item']
    ];
    
const numbers = [0,1,2,3,4,5,6,7,8]

let array =pickRandom(list);
let index = pickRandom(numbers);
1 Like

I tried to write

return arr[neighbor]

But it’s impossible
S. O Android

1 Like

Hey there,

There are 2 separate keys on the keyboard that look like [], an orange and a purple one. You’ll need to use the purple one.

The orange one is used to create a new array, and in the app will look like this when it is added to the code:

[
 ___,
]

The purple one is used to access an item in an array or an object property in bracket notation. This is the one you want to use here.

When added to the code, it will look like this:

[__]

We’re working on making the difference between these 2 keys more clear in the app, as it is understandably a bit confusing.

Hope this helps!
Ben

it works! , thanks :smile:

1 Like

Hi there,
Why wouldn’t the following work for this task? Is it that ++ can’t happen within array index?
Thank you

Sorry, but I don’t understand what the ‘arr’ parameter represents. A random number selected from the array?

Hey there, correct!

i++ is shorthand for i = i + 1, which is assigning a value to a variable. This can’t happen in an array index.

What you want to do to get the neighbor is arr[i + 1]

— Ben

Hey there, arr represents the array that the function is called with.

I dont know what Is wrong…
The message in my Solution Is:
The mine ítem Is undefined

The nine ítem Is undefined

The neighbor of ninth ítem Is undefined ,this Is the message of my Solution in this exercises,

What is my error

Looks like the variable name inside the import statement was changed. This can happen if two variables have the same name. If you rename one of them, then both will change.

Unfortunately, you can’t edit the import statement directly, so you’ll have to reset the puzzle and start over.

The rest of your code looks fine though, so you should be able to get past this pretty quickly.

Hope this helps!
Ben

Hey there, try posting a screenshot of your code and I can take a look.