Javascript algorithms Reverse a string

Tell us what’s happening:
Please help.
Whenever I run the test, I get it wrong but the console shows the right answers without the quotation marks. Would be much grateful

Your code so far


function reverseString(str) {
let arr = [];
let strarr = str.split("");
for (let i = strarr.length - 1; i >= 0; i --) {
arr.push(strarr[i]);
}
console.log(arr.join(""));
}

reverseString("Greetings from Earth");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36.

Challenge: Reverse a String

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/reverse-a-string

your function always return undefined

why?

Answer

because it doesn’t have a return statement.

2 Likes

Thank you very much, but whenever I run it I do not get undefined. Would try the return statement though.

to have printed to the console what the function return you need to put the function call in a console.log:


console.log(reverseString("Greetings from Earth"));

1 Like

Thanks :+1: @ilenia, it worked. I think i had a wrong view on console.log() and return.