Prep Katas Flashcards
Very simple, given a number, find its opposite.
function opposite(number) { //your code here }
//Additive Inverse ‘ - ‘
return -number;
Convert Boolean values to strings ‘Yes’ or ‘No’.
function boolToWord( bool ){ //your code here }
return bool? ‘Yes’:’No’;
Create a function that takes an integer as an argument and returns “Even” for even numbers or “Odd” for odd numbers.
function even_or_odd(number) { // ... }
//Modulus (remainder)
return number % 2 === 0 ? ‘Even : ‘Odd’ ;
Unfinished Loop - Bug Fixing #1
Oh no, Timmy’s created an infinite loop! Help Timmy find and fix the bug in his unfinished for loop!
function createArray(number){ var newArray = [];
for(var counter = 1; counter <= number; ){ newArray.push(counter); }
return newArray;
}
// sentry:variable that controls loop // Initialisation | Condition | Change
…
for(var counter = 1; counter <= number; counter ++ ){
newArray.push(counter);
}
…
Jenny has written a function that returns a greeting for a user. However, she’s in love with Johnny and would like to greet him slightly differently. She added a special case to her function, but she made a mistake.
function greet(name){ return "Hello, " + name + "!"; if(name === "Johnny") return "Hello, my love!"; }
//Exiting function too early. //Move return to the end of the function
if(name === “Johnny”) {
return “Hello, my love!”;
}
return “Hello, “ + name + “!”;
Give you a function animal, accept 1 parameter:obj like this:
{name:”dog”,legs:4,color:”white”}
And return a string like this:
“This white dog has 4 legs.”
function animal(obj){ return ? }
//String interpolation & Template Literals //Property Accessors.
return This ${obj.color} ${obj.name} has ${obj.legs} legs.
;
Write a function called repeatStr which repeats the given string string exactly n times.
function repeatStr (n, s) { return ''; }
//String.repeat(count);
return s.repeat(n);
// New variable, empty str //For loop n times: str+=s; //Return str;
//New variable, empty arr //For loop n time: str.push(s); //Return str.join('');
Complete the square sum function so that it squares each number passed into it and then sums the results together.
For example, for [1, 2, 2] it should return 9 because 1^2 + 2^2 + 2^2 = 9.
function squareSum(numbers){
}
// Array.reduce
return numbers.reduce((acc,num)=> acc + Math.pow(num,2),0);
//For loop let sumSquares = 0; for ( let i=0 ; i
Given a string, you have to return a string in which each character (case-sensitive) is repeated once.
function doubleChar(str) { // Your code here }
//arr.map
return str
.split(‘’)
.map(char=>char+char)
.join(‘’);
//for loop
let newStr =''; for ( let i=0; i
Consider an array/list of sheep where some sheep may be missing from their place. We need a function that counts the number of sheep present in the array (true means present).
function countSheeps(arrayOfSheep) { // TODO May the force be with you }
// arr.filter.length
return arrayOfSheep.filter(sheep=>sheep ===true).length;
Write a function findNeedle() that takes an array full of junk but containing one “needle”
After your function finds the needle it should return a message (as a string) that says:
“found the needle at position index”
function findNeedle(haystack) { // your code here }
// arr.indexOf(element)
let index = haystack.indexOf(‘needle’)
return index !== -1 ? found the needle at position ${index}
: ‘oops! no needle here’;
Simple, remove the spaces from the string, then return the resultant string.
function noSpace(x){
}
// str to arr and back to str
return x
.split(‘ ‘)
.join(‘’)
// str.replace
return x.replace(/\s/g,’’);
You take your son to the forest to see the monkeys. You know that there are a certain number there (n), but your son is too young to just appreciate the full number, he has to start counting them from 1.
As a good parent, you will sit and count with him. Given the number (n), populate an array with all numbers up to and including that number, but excluding zero.
function monkeyCount(n) { // your code here }
// For loop value of n
let countArr = []; for ( let i = 1 ; i <= n ; i++ ){ countArr.push(i); } return countArr;
Create a function with two arguments that will return an array of the first (n) multiples of (x).
Assume both the given number and the number of times to count will be positive numbers greater than 0.
Return the results as an array (or list in Python, Haskell or Elixir).
function countBy(x, n) { var z = [];
return z;
}
//for loop value of n
var z = []; for( let i = 1 ; i <= n ; i++ ){ z.push(x*i); } return z;
Write a function that takes an array of words and smashes them together into a sentence and returns the sentence. You can ignore any need to sanitize words or add punctuation, but you should add spaces between each word. Be careful, there shouldn’t be a space at the beginning or the end of the sentence!
[‘hello’, ‘world’, ‘this’, ‘is’, ‘great’] => ‘hello world this is great’
function smash (words) {
};
// arr.join
return words.join(‘ ‘)