Bake a Cake explainer


#1

The aim of this puzzle: Make a list (array) of ingredients (strings) needed to bake a cake.

Walkthrough of the solution: The cook() function is ready to turn ingredients into a cake but right now ingredients is just ‘flour’.

First, tap on the string ‘flour’ to highlight it. Next, tap the [] button to replace ‘flour’ with an empty array []. Now tap the str button to open the keyboard and type in the name of an ingredient like ‘sugar’ or ‘eggs’. Tap the empty space below the new ingredient you just entered and add another.

Once you have at least 2 strings inside the ingredients array, try running the code to bake your cake.

Sample code solution:
(Tap below to reveal)

var ingredients = [ 
    'flour', 
    'sugar', 
    'eggs', 
    'butter' 
];
cook(ingredients);

JavaScript Concepts: Data Structures (arrays), Variable Declaration, Calling Functions, Identifiers


#4

Did you just add this puzzle in late May? I went to look, and it was one which I hadn’t done. I also found a quiz I hadn’t done.


#5

@Scott_McNay — Yup! We’re continually improving the courses, and sometimes that means replacing, or adding, new Puzzles or Quizzes.


#6

Can i see the code running in the back? I’d there is any


#7
const cook = array => {
  if (Array.isArray(array) && array.length > 1 && !array.includes('')) {
    print('You baked a cake!');
  } else {
    print("That's not enough ingredients.");
  }
};

There’s quite a bit of code here that hasn’t been taught before the Bake a Cake puzzle. Basically, cook is a function, like pickRandom() or drawBox(). When you put an array inside of the parentheses of cook(), it checks 3 things:

  1. Is this really an array? It shouldn’t be just a number, or a string, or something else.
  2. Does the array have more than 1 item? It shouldn’t be empty or only have 1 item.
  3. Are any items in the array blank? There shouldn’t be any empty strings which is just quote marks without anything in between.

If the thing in the parentheses passes all 3 checks, then the message 'You baked a cake!' gets printed out. If it fails any of the tests, then it prints out 'That's not enough ingredients.'

–Frankie