JavaScript, inside out · Chapter 5 of 51

Strings

By the end of this chapter you can clean up, cut, search, split, pad and rebuild text, whatever way people typed it. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: First day at Tapri
  • 6 tasks
  • JavaScript
  • In the all labs plan

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.

Why you need this

Anjali has printed the first batch of order slips, and she is not happy. One says " aNJALI" because the customer typed with caps lock on, and another prints a full phone number for anyone in the queue to read. The kitchen wants short codes like MAS-007 instead of "masala chai, order 7", Ravi wants to spot a "less sugar" note however it is typed, and the new website needs a web address for every menu item.

So far you can glue text together with + and print it. That is not enough to change what is inside: there is no way yet to take the first three letters, fix the capitals, find a word, or hide some digits.

What you build: 6 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    cleanName: tidy a name however it was typed

    Write cleanName(raw). Remove the spaces at both ends, then return the name with its first letter in upper case and every other letter in lower case.

    cleanName(' aNJALI ') gives 'Anjali', cleanName('MEERA') gives 'Meera', and a name that is already tidy, like 'Kiran', comes back the same.

  2. Task 2

    orderCode: a kitchen code like MAS-007

    Write orderCode(item, orderNumber). Return the first three letters of the item in capitals, a dash, and the order number padded with zeros at the front to three digits.

    orderCode('masala chai', 7) gives 'MAS-007' and orderCode('Cutting chai', 42) gives 'CUT-042'. Spaces before the item do not count: orderCode(' bun maska', 142) gives 'BUN-142', and a number that already has three digits is not padded.

  3. Task 3

    wantsLessSugar: spot less sugar in any capitals

    Write wantsLessSugar(note). Return true when the note contains the words less sugar, in any mix of upper and lower case and anywhere in the note, and false otherwise.

    'Less SUGAR please' and 'extra ginger, LESS SUGAR' give true. 'extra ginger' and 'more sugar' give false.

  4. Task 4

    slugify: a web address slug for a menu item

    Write slugify(item). Return the item in lower case, with no spaces at either end and a dash in place of every space between words. Items have single spaces between their words.

    slugify(' Masala Chai Special ') gives 'masala-chai-special', slugify('Kulhad Chai With Extra Elaichi') gives 'kulhad-chai-with-extra-elaichi', and a dash already in the name stays: slugify('parle-g') gives 'parle-g'.

  5. Task 5

    maskPhone: hide all but the last four digits

    Write maskPhone(phone). phone is text. Return text of the same length in which every character except the last four is a star *, and the last four are the phone's own last four digits.

    maskPhone('9876543210') gives '****3210' and maskPhone('08041234567') gives '*****4567'. A phone of just four digits has nothing to hide: maskPhone('1234') gives '1234'.

  6. Task 6 · interview question

    Interview: reverseString, reverse a string

    Write reverseString(text). Return the same characters in reverse order: reverseString('chai') gives 'iahc' and reverseString('Tapri') gives 'irpaT'.

    One character comes back as it is, and empty text '' gives ''.

What you will learn

  • Positions and slice: count characters from 0, and copy any piece of a string.
  • Case and spaces: toUpperCase, toLowerCase and trim.
  • Searching and replacing: includes, indexOf, replace and replaceAll.
  • split, join and padStart: break text into pieces, glue pieces back, and pad to a fixed width.

More in First day at Tapri

Build "Strings" yourself

Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.