Silly Questions

My code just wont do what Im trying to do.
Im trying to only get a string, reverse it and join into a string.

it’s not working on the browser console. It returns the same string :frowning:

Code:

const reverseString = function(string) {
 let array = string.split();
 console.log(array);
 array.reverse();
 return array.join();
  
}
reverseString("ello wlao");

.reverse() doesn’t change the array. It returns a new one.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse

If I’m correct, isn’t reverse() not allowed to be used in this challenge?

You can spli() and reverse with a for() loop or you can use split() as it returns an array then you can map() your array with conditions to push() into a new array then return a join().

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

I don’t think @ArielLeslie this time saw the issue, as reverse does in fact reverse the array in place

The reverse method transposes the elements of the calling array object in place, mutating the array, and returning a reference to the array.

I would say the issue is with split

If separator is omitted or does not occur in str , the returned array contains one element consisting of the entire string.

(separator being the name for the argument)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split

how do you want to split the string?

A simple and efficient solution would be not to bother with making an array at all, but rather looping through the original string backwards and simply adding those characters in that order to a new string, and then return that string

function reverseString (string) {
    //string is the input, and the value you wish to reverse

    //newString will be the string you are constucting as a reversed form of string
    let newString = "";

    //A loop that can start at the end of the string, but I'll leave that up to you
    for () {
        //you should change newString such that it adds the values of string in here
    }   
    //newString at this point should be a reversed string 
    return newString 
}

As a bonus you won’t have to worry about any arrays

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.