FAQ: Arrays - Review Arrays

This community-built FAQ covers the “Review Arrays” exercise from the lesson “Arrays”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Review Arrays

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi there,

I was trying to compare elements within two arrays to see if the two are the same. But when I ran the following code, the result disagrees with my expectation:

let a = [1, 2, 3];
let b = [1, 2, 3, 4];

function compare(a,b) {
for(i=0; (i < 3 || i < 4; i++) {
if(a[i] === b[i]) {
return “same arrays”;
}
return “different arrays”;
}
}

console.log(compare(a, b)); //same arrays

I am wondering why the comparison didn’t turn out “different arrays” :frowning:

Hello Everybody,
I am pretty new at learning JavaScript so please be kind considering this basic question!
I want to take all the elements in an array and make a string. The results of my code below is not at all what I want as the console outputs “bb”. I have no idea why!

my code is :

let myArr = [‘a’, ‘b’, ‘c’, ‘d’];
let newString = “”;

for (i = 0; i< myArr.length; i += 1){
let myLetter = myArr.shift();
newString = myLetter + myLetter
}
console.log(newString);

Thanks a lot for your help, understanding why it outputs “bb”, and also some tips to reach my goal and make a string out of this array!

Hi cecilerx,

Regarding you question about taking all elements of an array and make a string, I believe you will find the following page quite interesting :slight_smile:

Hi, in the extra practice there is a task call :
Use the .length property to find the last element of an array.
How am I supposed to do to get the element but not the number of the length?

I was able to find the answer here: https://flaviocopes.com/how-to-get-last-item-array-javascript/

I don’t fully understand how it works myself, but it does work though.

const randomString = ['this', 'is', 'random'];

const result = randomString[randomString.length - 1];

console.log(result); //Prints: random

Hi zs-kev,

thank you so much for answering! Now I get it, actually it’s not hard to understand.
Arrays are zero-indexed, the index of the last element of an array is equivalent to the length of that array minus 1.
In this case, randomString.length equals to 3, and the index of the last item, ‘random’, is 2.
Hopefully it’ll make more sense to you.

No prob!

Yea, I do get what is going on with the .length - 1. What i don’t understand is why it is printing out the element and not the length of it? I don’t fully understand what is going on with this part:

randomString[randomString...]

From my understanding, .length prints out the length of the string or the array, so why do we get the result of ‘random’ and not 6?

This equal

const result = randomString[3 - 1];
const result = randomString[2];

We can access one item in an array using its index, with syntax: randomString[2]
so when I log result to console, the output will be the element random
Not sure what does your 6 represents, but does that answer your question?

1 Like

Ah okay, I see now how it works.

The reason I said why not 6 was because random consists of 6 characters, and from my understanding of the length property it prints out the length of a string or array, etc.

That is where I was getting confused, I wasn’t sure why when using .length, we weren’t getting the length of that string = 6 but instead, we were getting the string itself. The way you have described above has made it click.

So we are using it to find the position of that item that we want, and then we are logging the result of that position.

1 Like