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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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”
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, 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?
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.
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?
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?
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.
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!
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.
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.