2

const reverseArray = (words) => {
  for (let i = words.length - 1; i >= 0; i--) {
    console.log(`${words[i]}`);
  }
};

const sentence = ['sense.', 'make', 'all', 'will', 'This'];

console.log(reverseArray(sentence))

// Should print ['This', 'will', 'all', 'make', 'sense.'];

const nums = [1, 2, 3, 4, 5];

console.log(reverseArray(nums));

// Should print 5,4,3,2,1

But it keeps giving me Undefined at the end ? I know this is a rookie question , sorry.

8
  • 1
    Change the function so it returns the reversed array instead of logging it. Commented Dec 16, 2020 at 17:08
  • 1
    Does this answer your question? Chrome/Firefox console.log always appends a line saying 'undefined' Commented Dec 16, 2020 at 17:13
  • 1
    Your code is basically const reverseArray = (words) => { for (let i = words.length - 1; i >= 0; i--) { console.log(${words[i]}); } return undefined; }; Commented Dec 16, 2020 at 17:13
  • There are already several answers for this stackoverflow.com/questions/14633968/… Commented Dec 16, 2020 at 17:13
  • 1
    No.............. The person is literally logging undefined.... Commented Dec 16, 2020 at 17:15

4 Answers 4

3

It prints undefined in the end because you are trying to console.log function but it returns nothing there is no need for the console.log you can just call this function.

reverseArray(sentence)
reverseArray(nums)

But better way to reverse an array is to use build-in function reverse() So you can change your function like this.

const reverseArray = (words) => {
  words.reverse().forEach(item => {
    console.log(item);
  });
};
Sign up to request clarification or add additional context in comments.

7 Comments

The function is not correct. It does not reverse the array, nor returns anything. And if nothing is returned then it's not even considered a function, it's basically a method which is logging the array. See my answer.
@Zunair It reverse the array.
I meant in the question the reverseArray does not reverse the array.
@Zunair I am not a big expert in js, but if I define: function myFunction() {}. Can I call it a function if it doesn't return anything?
@Zunair Yes, you are right technically function doesn't reverse the array. Updated the comment. Thank you for your comment!
|
1

The problem is without a return, the function is returning nothing, which means undefined. So your console.log(reverseArray(sentence)) call is the same as console.log(undefined).

In this answer, the function just returns the reversed array.

const reverseArray = (words) => {
  reversed = []
  for (let i = words.length - 1; i >= 0; i--) {
     reversed.push(words[i])
  }
  return reversed
};

const sentence = ['sense.', 'make', 'all', 'will', 'This'];

console.log(reverseArray(sentence))

// Should print ['This', 'will', 'all', 'make', 'sense.'];

const nums = [1, 2, 3, 4, 5];

console.log(reverseArray(nums));

3 Comments

The code is correct here, but see my answer for more details.
imvain2 , this was the correct response and it makes sense so i'm creating a reversed var as an empty array and returning the loop to that. Got it , I was close but not quite there. Thank you community for the help. This was my first question. The built in method .reverse would have worked also
You can still vote on other answers, also you can look up these kind of questions.
1

Your reverseArray function doesn't return an array or anything hence undefined.

And it doesn't reverse your array and does not store it anywhere.

You need to reverse the array into a variable then return it at the end of the function.

Or just use built-in string.reverse() method to reverse the array!

Comments

-1

Every function in JavaScript returns undefined unless you return something else.

Try:

const reverseArray = (words) => {
  for (let i = words.length - 1; i >= 0; i--) {
     console.log(`${words[i]}`);
  }
  return "" //Here magic occurs
};

const sentence = ['sense.', 'make', 'all', 'will', 'This'];

console.log(reverseArray(sentence))

// Should print ['This', 'will', 'all', 'make', 'sense.'];

const nums = [1, 2, 3, 4, 5];

console.log(reverseArray(nums));

3 Comments

If it does not return anything, it's called a method.
it does not matter i got -1 anyway people are providing answers that he did not ask..
You can edit the answer and explain a bit more - just say see comment below. Not everyone reads each line. I like your way, since it's makes them figure it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.