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);

I tried to write

return arr[neighbor]

But it’s impossible
S. O Android


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!


it works! , thanks :smile:


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.

1 Like

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!


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

1 Like


Hey there, the return keyword tells the function to stop executing and return a value. Once the function sees a return statement, any code after it will not run.

Your function returns arr[neighborIndex], but this line is running before neighborIndex is declared, so the computer doesn’t know what neighborIndex is. Swap those 2 lines, then delete the 2nd and 3rd return statement.


So, do arr and i always represent array and index? Or why is it so in this case?

Thanks for your help!

Just a quick note that if you create a variable “neighbor” instead of “neighborIndex”, everything works fine!

arr and i are the names of the parameters. It helps to use descriptive names for the parameters an d everything else. But you don’t have to use these names.

You could call the variable anything. But maybe neighbor is a better name, because we want to return the item and not its index. Also, the function is called getNeighbor so it is nice if the variable name matches up with that. Thanks for joining the conversation!