Indonesian Flag Fixer explainer

Aim of the puzzle: Use the .replace() function to fix a mistake in a string.
Walk through of solution: On line 1, the variable flag contains the string 'rrr wwg'. On line 2, this string will be used as an argument for the drawBoxes() function.

The flag of Indonesia has 2 colors: a stripe of red and a stripe of white. Unfortunately, the ‘g’ in ‘rrr wwg’ means that DrawBoxes() will mistakenly put a green box in the flag. We can use the .replace() function to fix this mistake. Inside the drawBoxes() function, .replace() is added to the end of flag. This function will look for a target string inside flag, and replace it with a new string.

Inside the parentheses, .replace() takes 2 arguments, the target string 'g', and the replacement string 'w'. When the code runs, .replace() will find the 'g' in 'rrr wwg', and replace it with a 'w'. Then drawBoxes() will draw the correct flag of Indonesia.

It is important to note that the .replace() function will only replace the first match it finds. If the string contained two 'g's, only the first (farthest left) would be replaced. This post explains how one could “replace all”.

Sample code solution:

var flag = 'rrr wwg';
drawBoxes(flag.replace('g', 'w'));

Javascript Concepts: Variables, Strings, .replace()
Grasshopper Concepts: drawBoxes()