Start learning free

LESSON 73 OF 285 · STRINGS - CHALLENGES

Amazing to Not Amazing in JavaScript: slice

Practice Read the full explanation

Given a word s that starts with the letter a, return "not " followed by s without its first character. That's how "amazing" becomes "not mazing"!

 

Example 1:

Input: s = "amazing"
Output: "not mazing"
Explanation: Without its first character, "amazing" is "mazing". "not " goes in front of it, with its space.

Example 2:

Input: s = "aMAZING"
Output: "not MAZING"
Explanation: Only the first character is removed. The other characters stay as they are, upper case included.

Example 3:

Input: s = "an"
Output: "not n"
Explanation: Removing the "a" leaves just "n".

 

Constraints:

  • s has at least 2 characters and starts with a lower-case a.
  • s is one word made of English letters, lowercase or uppercase (no spaces).
STEP 1 OF 1
    Ctrl + Enter to check

    JAVASCRIPT · STRINGS - CHALLENGES

    Amazing To Not Amazing in JavaScript, explained

    Problem overview

    The method amazingToNotAmazing receives a word s that starts with the letter a. It returns "not ", space included, followed by s without its first character. That turns "amazing" into "not mazing".

    Only the first character is removed. "aMAZING" becomes "not MAZING", with the capitals kept, and "an" becomes "not n".

    Approach

    The task has two parts: remove the first character, then put a fixed prefix in front. The string method slice(start) returns the part of a string from index start to the end. Since indexes start at 0, s.slice(1) is everything except the first character. It returns a new string and leaves s unchanged, because strings in JavaScript can't be modified.

    Step by step

    1. Slice from index 1. With no end index, slice runs to the end of the string.
    2. Add the prefix "not " with + and return the result.

    What slice does

    For "amazing", which has 7 characters, slice(1) returns "mazing". slice(0) returns the whole word, "amazing". With two arguments the second one is where the slice stops, and that index is not included: "amazing".slice(1, 3) is "ma". Leaving the second argument out is what makes the slice run to the end, so the lesson needs no length at all.

    The solution in JavaScript

    class Solution {
        amazingToNotAmazing(s) {
            return "not " + s.slice(1);
        }
    }
    

    On the three examples it returns:

    "amazing" -> "not mazing"
    "aMAZING" -> "not MAZING"
    "an"      -> "not n"
    

    Strings don't change

    Calling slice does not modify the word. After let t = s.slice(1) with s = "hello", t is "ello" and s is still "hello". Every string method that seems to change a string really returns a new one, so the result has to be used, here in the return.

    The method substring(1) gives "mazing" for "amazing" as well. The lesson uses slice, which also works on arrays and accepts negative indexes.

    Thinking about the problem

    When a task says "without its first character", look for a way to ask for "everything from index 1". That is the shape of slice(1). When it says to put something in front, that is + with the fixed text on the left. The order of the operands decides the order of the result: s.slice(1) + "not " would put the prefix at the end, giving "mazingnot " for "amazing".

    The word always starts with a, so the code never has to check that the removed character is an a. It removes whatever is first.

    Try the function with a variable in the editor above and print the result, to see the exact text, space included. A missing space is easy to miss on screen but fails the lesson's tests.

    Edge cases and common mistakes

    • Forgetting the space. The space belongs inside the quotes. With "not" + "amazing".slice(1) the result is "notmazing".
    • Slicing from 0. s.slice(0) keeps the whole word. The first character is index 0, so the slice has to start at 1.
    • Changing the letters. Nothing is converted to lower or upper case: the remaining characters are copied as they are.

    Testing

    • "ab" gives "not b".
    • "aZ" gives "not Z": an upper-case letter after the first character stays upper case.

    Both results are what the solution returns when you run it.

    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free