1

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.

blex
  • 24,941
  • 5
  • 39
  • 72
blight231
  • 23
  • 5
  • 1
    Change the function so it returns the reversed array instead of logging it. – Barmar Dec 16 '20 at 17:08
  • 1
    Does this answer your question? [Chrome/Firefox console.log always appends a line saying 'undefined'](https://stackoverflow.com/questions/14633968/chrome-firefox-console-log-always-appends-a-line-saying-undefined) – Love2Code Dec 16 '20 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; };` – epascarello Dec 16 '20 at 17:13
  • There are already several answers for this https://stackoverflow.com/questions/14633968/chrome-firefox-console-log-always-appends-a-line-saying-undefined – Love2Code Dec 16 '20 at 17:13
  • @Love2Code that is not the same.... This is not the extra log line in the console from executing console.log() – epascarello Dec 16 '20 at 17:13
  • @epascarello It is somewhat the same problem, as well as this one https://stackoverflow.com/questions/24342748/why-does-console-log-say-undefined-and-then-the-correct-value – Love2Code Dec 16 '20 at 17:15
  • 1
    No.............. The person is literally logging undefined.... – epascarello Dec 16 '20 at 17:15
  • Without returning anything `console.log(reverseArray(sentence))` is basically the same as `console.log(undefined)`, but it just happens to be running a function also. – imvain2 Dec 16 '20 at 17:23

4 Answers4

2

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);
  });
};
laurisstepanovs
  • 432
  • 5
  • 14
  • 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 Dec 16 '20 at 17:39
  • @Zunair It reverse the array. – laurisstepanovs Dec 16 '20 at 17:44
  • I meant in the question the reverseArray does not reverse the array. – Zunair Dec 16 '20 at 17:46
  • @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? – laurisstepanovs Dec 16 '20 at 17:48
  • @Zunair Yes, you are right technically function doesn't reverse the array. Updated the comment. Thank you for your comment! – laurisstepanovs Dec 16 '20 at 17:54
  • Well technically in js method is a function. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain for better understanding. – Zunair Dec 16 '20 at 17:55
  • I think @blight231 is trying to learn how to make a function which reverses an array. We should guide how to do that and we can show what should be the best approach. – Zunair Dec 16 '20 at 17:58
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!

Zunair
  • 1,085
  • 1
  • 13
  • 21
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));
imvain2
  • 15,480
  • 1
  • 16
  • 21
  • The code is correct here, but see my answer for more details. – Zunair Dec 16 '20 at 17:42
  • 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 – blight231 Dec 16 '20 at 18:34
  • You can still vote on other answers, also you can look up these kind of questions. – Zunair Dec 16 '20 at 18:36
-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));
Shivam
  • 1
  • 3
  • If it does not return anything, it's called a method. – Zunair Dec 16 '20 at 17:44
  • it does not matter i got -1 anyway people are providing answers that he did not ask.. – Shivam Dec 16 '20 at 18:16
  • 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. – Zunair Dec 16 '20 at 18:34