Start learning free

LESSON 238 OF 675 · SETS

Removing from a Set and Looping Over It in TypeScript

Lesson

TL ; DR:

delete(x) takes x out of a set. A for...of loop visits every value once.


let colors = new Set<string>(["red", "blue", "green"]);

colors.delete("blue");

colors.delete("pink");          // not in it, nothing happens

for (let color of colors) {
    console.log(color);         // red, then green
}


Full lesson:

s.delete(x) removes x from the set s. It returns true if x was there. If it wasn't, nothing happens and it returns false.

for (let x of s) visits each value of the set once.

A TypeScript set loops in the order the values were added. Most languages don't promise any order for a set. So a set is best for work where the order doesn't matter. A total, a count and a check are fine.

A set has no indexes. A for loop with i and s[i] doesn't work on it. Use for...of.


Your task

In a word game, the set found holds the words a player found. The words "qqq" and "zzz" aren't allowed. Remove both from found.

The player never found "zzz", so removing it changes nothing.

Then add up the points. A word scores one point per letter. Loop over found and add each word's length to score.

The code already prints how many words are left, and the points.

Expected output:

4 words
14 points
STEP 1 OF 3
    Ctrl + Enter to check