DEV Community

Ryver
Ryver

Posted on

Exploring JavaScript Algorithms: A Beginner’s Guide

Welcome, fellow developers! Today, we’re diving into the world of algorithms, unraveling the mysteries of three fundamental challenges, and offering multiple solutions to each. Whether you’re a coding enthusiast or a seasoned developer, these algorithms will sharpen your problem-solving skills and deepen your understanding of JavaScript.

Reverse a String

Reversing a string is the bread and butter of algorithmic challenges. Let’s explore a few ways to crack this nut with JavaScript.

Directions

Given a string, return a new string with the reversed order of characters.

reverse('apple') === 'leppa'
reverse('hello') === 'olleh'
reverse('Greetings!') === '!sgniteerG'
Enter fullscreen mode Exit fullscreen mode

Solution(s)

The Quick and Easy Way:

function reverse(str) {
  return str.split("").reverse().join("");
}
console.log(reverse("apple")); // Output: 'leppa';
Enter fullscreen mode Exit fullscreen mode

Using .reduce():

function reverse(str) {
  return str.split("").reduce((rev, char) => char + rev, "");
}
console.log(reverse("apple")); // Output: 'leppa';
Enter fullscreen mode Exit fullscreen mode

Using a for of Loop:

function reverse(str) {
  let reversed = "";

  for (let character of str) {
    reversed = character + reversed;
  }

  return reversed;
}
console.log(reverse("apple")); // Output: 'leppa';
Enter fullscreen mode Exit fullscreen mode

Reverse an Int

Reversing an integer is a close cousin to reversing a string, but with a twist — we need to manage data types.

Directions

Given an integer, return an integer that is the reverse ordering of numbers.

Examples

reverseInt(15) === 51
reverseInt(981) === 189
reverseInt(500) === 5
reverseInt(-15) === -51
reverseInt(-90) === -9
Enter fullscreen mode Exit fullscreen mode

Solution(s)

Using parseInt():

function reverseInt(n) {
  const reversed = n.toString().split("").reverse().join("");

  return parseInt(reversed) * Math.sign(n);
}
console.log(reverseInt(15)); // Output: 51
Enter fullscreen mode Exit fullscreen mode

Capitalize a String

Now that we’ve tackled reversals, let’s add some flair by capitalizing a string.

Directions

Write a function that accepts a string. Capitalize the first letter of each word in the string and return the capitalized string.

Examples

capitalize('hello, there!') --> 'Hello, There!'
capitalize('a lazy fox') --> 'A Lazy Fox'
capitalize('look, it is working!') --> 'Look, It Is Working!'
Enter fullscreen mode Exit fullscreen mode

Solution(s)

With a _for_ Loop:

function capitalize(str) {
  let result = str[0].toUpperCase();

  for (let i = 1; i < str.length; i++) {
    if (str[i - 1] === " ") {
      result += str[i].toUpperCase();
    } else {
      result += str[i];
    }
  }

  return result;
}
console.log(capitalize("hello, there!")); // Output: 'Hello, There!'
Enter fullscreen mode Exit fullscreen mode

With a for of Loop:

function capitalize(str) {
  const words = [];

  for (let word of str.split(" ")) {
    words.push(word[0].toUpperCase() + word.slice(1));
  }

  return words.join(" ");
}
console.log(capitalize("hello, there!")); // Output: 'Hello, There!'
Enter fullscreen mode Exit fullscreen mode

And That’s it:

And there you have it! Three simple yet powerful algorithms conquered with the magic of JavaScript.

Top comments (0)