Thank you for your reply! Got it.
Anyway, I am moving slow and even so I understand well several topics, without immidiate practice it is becoming a little confusing.
Appreciated.
I have a few things I don’t understand about the functional code below:
the arr is a variable that represents 1 item in the array, but I don’t see how the code understands that arr refers to the aliens array. It is linked nowhere. In my logic ‘arr’ would need to be replaced by ‘aliens’ everywhere for this to function but this is not necessary.
how is it irrelevant if the array lives above or below the for loop function? If you place the array after the for loop you’d think that it would get stuck for not having the array ‘available’.
const greetAliens = arr => {
for (let i = 0; i < arr.length; i++) {
console.log('Oh powerful ' + arr[i] + ', we humans offer our unconditional surrender!');
}
}
const aliens = ["Blorgous", "Glamyx", "Wegord", "SpaceKing"];
greetAliens(aliens)
I used two different methods to get the output the lesson requested, but neither one got a check box. m I’ll be honest it drove me nuts for a good 30 mins. I ended up just clicking the get solution (which made me mad lol). Why didn’t either of my functions pass?
function greetAliens(arr){
for (let i = 0; i < aliens.length; i++) { // for loop conditions
console.log(`Oh powerful ${aliens[i]}, we humans offer our unconditional surrender!`)
};
}
// alternative solution using for...of
/*
const greetAliens = (arr) => {
for (const x of aliens){
console.log(`Oh powerful ${x}, we humans offer our unconditional surrender!`)
};
}
*/
const aliens = ["Blorgous", "Glamyx", "Wegord", "SpaceKing"];
greetAliens(aliens)
things do not go as planned. I expected to see my doctor who inspired aliens (cybermen, daleks and so forth). That is not happening. That should work. See if you can figure this out, otherwise I will help you further.
for the first code sample, you attempt to use function declaration, here is some documentation to help you:
I updated the code, my mistake with the ‘=’ in the first function, as I copy/pasted from my vscode copy of my code and I didn’t notice the error, this was detected in the codecademy editor.
I tried this a couple of ways in Visual Studio Code. greetAliens() works fine in both Codecademy and VSC. With greetAlien() only the first element from the array gets logged to the console.
No. It is just old school, sort of. for..of is new, but implementation of the for loop is not.
When we just want to do something with each element in an array, Array.forEach() is rather sweet since we can write multiple utility functions for various purposes and use them as the callback.
greeting = alien => {
console.log(`Oh powerful ${alien},
we humans offer our unconditional surrender!`)
}
farewell = alien => {
console.log(`Farewell ${alien},
we humans are glad to see you go!`)
}
aliens.forEach(greeting);
aliens.forEach(farewell);
for
(let i = 0; //This part creates a variable called 'i' and sets it equal to 0
i < aliens.length; //This part in full would be 'if the statement 'i' < (is less than) aliens.length returns true do the next part.
i++; //This part increases i by 1 each time it is run.
The first run through i = 0.
Second run through i = 1.
Third i = 2.
Fourth i = 3.
As there are four objects in the array, the for statement stops. It will attempt a fifth run through but that would mean that i = 4 which is no longer less than aliens.length.
Another way of thinking about it, which might help visualise is:
const greetAliens = (alien) => {
let i = 0;
while (i < alien.length) {
console.log(`Oh powerful ${alien[i]}, we humans offer our unconditional surrender!`);
i = i + 1;
}
};