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!

2 Likes

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:

2 Likes

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?

1 Like

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
2 Likes

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.

1 Like

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

As I was reviewing arrays, I was told to " * Find the return value of calling .push() on an array."
Here is my code, in which the output in the console turned out to be 3.

console.log(newYearList.push());

I am not sure if I did this correctly… if anyone can clarify what this question is asking us to do, I’d really appreciate it! :grin:

1 Like

alternatively, you may do

randomString.at(-1); // returns 'random'

see also: Array.prototype.at() - JavaScript | MDN

1 Like

I was also confused what this question was asking. Is it asking us to log to the console the value of the element we push to the array? Or is it asking for us to log the changed array after the push?

why would i want to use anything other than const when creating variables? it seems like let and var can pollute the global and local namespaces, but const seems like a consistently safe bet to avoid bugs. is there any reason to use let and var anymore in JS?

Because you can’t reassign values with constants, sometimes you will need to use let. A common example is in for loop declarations: for let i = 0; i < 10; i++).

As for var, it’s not used as much anymore since ES6 came about.

Not sure about others, but I certainly did! :wink:

This is how I interpreted it as well and solved it using the same method as you.

Hi,
Did anybody tried at() method in this task?
I’m having a problem with it.

The code is:

let store = [‘clothes’, ‘food’, 3, true];

let lastOne = store.at(3)

console.log(lastOne);

//TypeError: store.at is not a function

And if I’m trying an example code from here Array.prototype.at() - JavaScript | MDN
it gets me the same error. What am I doing wrong?
Thanks

The JS behind the LE is likely not a new enough release to include support for that relatively new method. Try using one of the supported methods (or bracket notation) to complete this exercise.

ok, thank you) at least now I know that it’s not some stupid mistake of mine

1 Like