FlipFlop (Super Edition)
Learning Competencies
- Use strings, integers, arrays, functions
- Use if/else or switch statements, string methods, for loops
Time Box
Activity | Time |
---|---|
Kata | 4 hours |
Reflect | 10 minutes |
Summary
FlipFlop is a wacky challenge. We're going to write a function that takes a number as an argument, if that number is a multiple of 3 the function will return 'Flip'. For multiples of 5 we will return 'Flop'. For numbers that are multiples of both 3 and 5, we will return 'FlipFlop'. If none of these cases are true, we're just going to return the number.
// For example:12Flip4FlopFlip78FlipFlop11Flip1314FlipFlop16// etc.
A key part of solving this challenge is using the remainder operator.. For example, if we wanted to check if something is divisible by 3, we can use the remainder operator like this: if (i % 3 === 0)
.
So let's do it.
First, write a flipflop
function which takes a number as its input. It should return the number that is passed to it, unless the number is:
- divisible by
15
, then it should be replaced with the string"FlipFlop"
- divisible by
5
, then it should be replaced with the string"Flop"
- divisible by
3
, then it should be replaced with the string"Flip"
Next, write a function called superFlipFlop
which takes an array
of numbers as its input.
It should loop over that array and then return a "FlipFloped" array
i.e. the array is identical to the original, but with the following changes:
For example:superFlipFlop([1,2,3]) // will return [1, 2, "Flip"]superFlipFlop([1,2,5]) // will return [1, 2, "Flop"]superFlipFlop([1,2,15]) // will return [1, 2, "FlipFlop"]superFlipFlop([30, 9, 20, 1]) // will return ["FlipFlop", "Flip", "Flop", 1]
Plan your code
For this challenge, we're going to start by practising our pseudocode. Before we can code complex functions, we first need to think through the steps we need them to do. This can take some time and brainpower to think through now but saves you time when it comes to writing your code.
To start, think through what you need the function to do and write pseudocode comments for the steps your function will take. These comments will not be written in code but plain English, as if you were talking a friend through the steps.
Write your initial solution
Write code to match your pseudocode steps. You may find you have to do things in a different order than you originally wrote in your pseudocode or add more steps in, and that's alright! The pseudocode is a first go. You don't have to match it exactly.
Run the tests
Once written, run the tests. If your initial solution passes all tests, move on to the next part.