FAQ: Code Challenges: Intermediate JavaScript - greetAliens()

Hi there, this is my current code for this challenge:

const greetAliens = (arr) => {
  for (let i = 0; i < arr.length; i++) {
    return `Oh powerful ${arr[i]}, we humans offer our unconditional surrender!`
  };
}

Now, if I use console.log() instead of “return”, the code works. I think I have an idea why, but wanted to make sure. Is it because after a “return” statement, the for loop is terminated and therefore, no more items of the array are checked?

Thanks for your opinion!

exactly, return hands back data to the caller, which signals that the function is done executing, even if the loops need to be broken/interrupted.

1 Like

Hey Allenvik

I had the same problem.
The error is that you have forgotten to give the method greetAliens() its parameter bewteen the parentheses.
The reason why its working, its because you used the array variable that is within the globel scope.

1 Like

those iterators are not yet a 1st thought. Nice drilling.

I read the responses here, but I could not find an explanation to why my for…of loop wasn’t accepted as a correct answer. It prints the same exact answer. Am I missing something?

const greetAliens = () =>{
  for(word of aliens){
    console.log(`Oh powerful ${word}, we humans offer our unconditional surrender!`);
  }
}

greetAliens(aliens);

Where is the aliens variable from?

from the array in the original problem. I’m sorry for not including it.

const aliens = ["Blorgous", "Glamyx", "Wegord", "SpaceKing"];

const greetAliens = () =>{
  for(word of aliens){
    console.log(`Oh powerful ${word}, we humans offer our unconditional surrender!`);
  }
}
greetAliens(aliens);

Here you go.

you are using a global variable within your function. You should use a parameter, so we can call the function multiple times with different arguments

1 Like

Thank you for the assistance. I had to re-read the task to realize that the goal of the code block was to allow us to pass any array not just aliens.

1 Like

why is it not passing ?

const greetAliens = (array)=>{

for(let i = 0; i<greetAliens.length; i++){

console.log('Oh powerful' +aliens[i] +'we humans offer our unconditional surrender!');

}

}

greetAliens is the name of the function. The parameter is, array.

1 Like

variant1 / for(i = 0; i < array.length; i++):

const greetAliens = (array) => {
  for(i = 0; i < array.length; i++){
    console.log(`Oh powerful ${array[i]}, we humans offer our unconditional surrender!`);
  }
}

variant2 / for…of :

const greetAliens = (array) => {
  for(let ele of array){
    console.log(`Oh powerful ${ele}, we humans offer our unconditional surrender!`);
  }
};

I dont understand, why my code is accepted as correct answer? I’ve looked at the solution and the only difference i found was the name of the array “aliens” vs. “arr”, what am I missing here?

// Write your code here: greetAliens = () => { for (let i = 0; i < aliens.length; i++) { console.log("Oh powerful " + aliens[i] + ", we humans offer our unconditional surrender!" ); } }; // When you're ready to test your code, uncomment the below and run: const aliens = ["Blorgous", "Glamyx", "Wegord", "SpaceKing"]; greetAliens(aliens);

So, I know your message is a few months old, but I thought I would add a reply in case someone comes along later with the same confusion.

In your version, the condition is assigned to aliens.length, specifically targeting the aliens array provided in the exercise. It works because that’s the only array we are working with.

However, let’s imagine a scenario where there was a step 2 to the exercise where we needed to create a new array with 2 more aliens and use the same callback function on it. In this case, the loop would still run for the length of the aliens array and log 2 undefined results in the console.

By giving the arrow function a parameter of arr (a shorthand for array) and using it for the conditional and string text, it means that whatever array we callback, whether it’s 1 index or 1000, the loop will adjust to the array’s requests, not only the 4-index length of the original aliens array."

I hope this makes sense.

So this is my code but it seems like it is not passing. To me it feels like a more “simple” or logical way of doing it. Could someone explain why is my code not okay? Thanks!

function greetAliens () { for (const alien of aliens){ console.log(`Oh powerful ${alien}, we humans offer our unconditional surrender!`) } }; const aliens = ["Blorgous", "Glamyx", "Wegord", "SpaceKing"]; greetAliens(aliens);

Does your function have a parameter?
What if the array had been assigned to a variable with a different name than aliens? Would your code still work? Why or why not?

Suppose I do this,

function greetAliens() {
    // Your code here
}

const aliens = ["Blorgous", "Glamyx", "Wegord", "SpaceKing"];
greetAliens(aliens);
// Seems to work

const aliensNicks = ["Blo", "Gla", "Weg", "SpcKg"];
greetAliens(aliensNicks);
// Doesn't seem to work correctly. 
// It still prints out the full names in the greetings instead of the nicknames. 
// Why?

Think about the relation between parameters and arguments.

1 Like

Makes a lot of sense, thank you for the helpful answer!

1 Like