DEV Community

Ben
Ben

Posted on

Code Challenges pt. 1

Going from beginner on up, this will become a long series of posts covering coding challenge questions and answers.

The real goal here is to give myself some extra practice as well as build a reference to many different challenge questions.

Primary languages = JavaScript/TypeScript, may include some Java as well.

First up, Reverse a String

Prompt:
Given a string, return a new string with the characters in reversed order.

Example outcomes:
reverseString('orange') === 'egnaro'
reverseString('hello world') === 'dlrow olleh'
reverseString('How About That?') === '?tahT tuobA woH'

Code:

Version 1 (using built-in string methods)
function reverseString(string) {
    return string.split('').reverse.join('')
}
Enter fullscreen mode Exit fullscreen mode

Why does this work?

First, our function takes in a string and uses the split() method on it.

The first argument you give split() is the separator - the character in the string that you want the string to be split on.

By giving it an empty string (note: '' does not have a space between it), you're telling it to split the string at each and every character.

But split() returns an array of strings, so if we split "Hello" for example, it would return ["H", "e", "l", "l", "o"].

And that's how we get to our next step - the .reverse() method.

.reverse() is not a string method, but an array method, which is why we can only work with it after .split() returns our array.

Suffice it to say, that does most of the work on this problem.

The last step is another array method - .join() - which we essentially can think of as doing the opposite of .split() here.

The .join('') method concatenates the strings in the array when given an empty string, and returns a string from the array method as well.

Version 2 (for-loop version)
function reverseString(string) {
    let revString = ''
    for (let i = 0; i < string.length; i++){
        revString = string[i] + revString
    }
    return revString
}
Enter fullscreen mode Exit fullscreen mode

Why does this work?

In the for-loop version, first we declare a new, empty string.

The idea here is that we have this empty string, and then as we loop through each character of our given string, we add the current character to the left of reversed string (revString).

Now let's pretend our string is "Hello".

The first time through the loop, revString is empty.

We enter our for loop, and we're telling it to loop through the string from left to right.

Since revString is empty, so we set it equal to the first character of the string (string[i] - because i starts at index 0), plus the current revString value (which is empty).

This sets revString to 'H' as we begin our second iteration through the loop.

Next time through, revString = 'H', string[i] = 'e', so revString = 'e' + 'H', which becomes 'eH'.

To illustrate one last loop now revString = 'eH', string[i] 'l', so revString = 'l' + 'eH', which becomes 'leH'.

And you get the rest :)

Next up, Palindromes...

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Using split is not really a good idea. Try your code with this string:

Hello World 😊
Enter fullscreen mode Exit fullscreen mode

A better way is to do the following - it still falls down in some cases, but is much better than using split:

const reverseString = s => [...s].reverse().join('')
Enter fullscreen mode Exit fullscreen mode