Start learning free

LESSON 75 OF 289 · STRINGS - CHALLENGES

Amazing to Not Amazing in TypeScript

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

    TYPESCRIPT · STRINGS - CHALLENGES

    Amazing To Not Amazing in TypeScript, explained

    Problem overview

    The method amazingToNotAmazing receives a word s that starts with a lower-case a and returns "not " followed by the word without its first character. The word "amazing" becomes "not mazing". Only the first character goes away: "aMAZING" becomes "not MAZING", with the upper-case letters unchanged. The shortest input, "an", gives "not n".

    Approach

    The task has two parts: build "the word without its first character", then put a fixed prefix in front of it. The slice method of a string returns a part of it. s.slice(1) starts at index 1 and, with no end index, runs to the end of the string. That is every character except the first.

    Step by step

    1. Cut off the first character with s.slice(1).
    2. Put "not " in front with +. The space belongs inside the quotes: without it, "amazing" would become "notmazing".
    3. Return the joined string.

    The solution in TypeScript

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

    Strings in TypeScript can't be changed in place. s.slice(1) doesn't remove anything from s: it builds a new string and leaves s as it was. After w.slice(1) on let w = "amazing", the variable w still holds "amazing". That is why the result has to be used, here by joining it and returning it.

    Tracing the example

    For s = "amazing", s.slice(1) is "mazing". Putting "not " in front gives "not mazing". For "aMAZING" the slice is "MAZING" and the result is "not MAZING".

    Other ways to write it

    s.substring(1) also gives "mazing" here. A template string builds the same result in one piece: `not ${s.slice(1)}` is "not mazing". Both are fine; the lesson uses + because it shows the two parts clearly.

    The method doesn't check that the word starts with a. The lesson guarantees it, and the function removes the first character whatever it is.

    How slice works

    slice(start, end) takes the characters from start up to, but not including, end. For "amazing":

    • slice(1) has no end, so it runs to the last character and gives "mazing";
    • slice(1, 3) stops before index 3 and gives "ma".

    A start index beyond the end of the string is not an error: slice returns an empty string. The original string is never modified, so the same s can be used again after the call. Because strings are immutable, "removing the first character" always means "building a new string without it".

    Building the answer takes one expression, so there is no need for a variable. A named variable for the slice would also work and can make longer code easier to read, but here the single return line already reads like the task.

    Edge cases and common mistakes

    • Forgetting the space in "not ". The result would run the two words together.
    • Using slice(0) or slice(2). slice(0) keeps the whole word, and slice(2) drops two characters, so "amazing" would become "not azing".
    • Changing the case. The rest of the word is kept exactly as given, so there is no need for toLowerCase.
    • The shortest word. "an".slice(1) is "n", so the function works down to two characters.

    Testing

    • "amazing" gives "not mazing".
    • "aMAZING" gives "not MAZING".
    • "an" gives "not n".
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free