Skip to content
Advertisement

This code keeps returning ‘undefined’ for (names[i]). what am I doing wrong?

This code keeps returning ‘undefined’ for (names[i]). When I console.log() it loops through the array and prints Hello (names[i]) or Good Bye (names[i])what am I doing wrong?

JavaScript

Advertisement

Answer

Your issue is that you’re passing the name in as an argument to the speak function, however, in your definition of speak it doesn’t take an argument at all.

You can fix it by defining it like this:

JavaScript

Also, here I cleaned up the logic for you.

JavaScript

Your other issue is that your speak function wasn’t returning anything, it was returning undefined implicitly.

example:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement