Sizing Up explainer


#1

The aim of this puzzle: Replace the 'M' and 'L' shirt sizes with 'XL'.

Walkthrough of the solution: The .splice() method can be uses to insert, remove, or replace sections of an array. It takes 3 arguments:

  1. The starting index of your selection
  2. The number of elements to select starting from that index
  3. The new elements to insert at the starting index

To remove the 'M' and 'L' shirt sizes, we need to know their indexes. We can see that 'M' is at index 1, so startPoint should be set to 1.

Since we want to replace 2 elements, the 2nd argument of .splice() will be 2. Set the value of itemsToReplace to 2.

The last argument is the new element to insert at the startPoint. It should stay as 'XL'.

Sample code solution:
(Tap below to reveal)

import { shirtSizes } from 'grasshopper.store';

let startPoint = 1;
let itemsToReplace = 2;
let newItem = 'XL';

console.log('old: ' + shirtSizes);
shirtSizes.splice(startPoint,itemsToReplace,newItem);
console.log('new: ' + shirtSizes);

JavaScript Concepts: Binary Expression (+ concatenation), console.log(), import, .splice()


#2