CodeWars Flashcards
You have to write a function that accepts three parameters:
-CAP is the amount of people the bus can hold excluding the driver.
-ON is the number of people on the bus excluding the driver.
-WAIT is the number of people waiting to get on to the bus excluding the driver.
If there is enough space, return 0, and if there isn’t, return the number of passengers he can’t take.
function enough(cap, on, wait) { if (on + wait < cap){ return 0; } else { return (on + wait) - cap; } }
A hero is on his way to the castle to complete his mission. However, he’s been told that the castle is surrounded with a couple of powerful dragons! each dragon takes 2 bullets to be defeated, our hero has no idea how many bullets he should carry.. Assuming he’s gonna grab a specific given number of bullets and move forward to fight another specific given number of dragons, will he survive?
Return True if yes, False otherwise :)
hero = (bullets, dragons) =>{ if (bullets/2 >= dragons) { return true; } else { return false; } }
Create a function that takes in a number. Console log all values from 1 to that number but if the number is divisible by 3 log “fizz” instead of that number, if the number is divisible by 5 log “buzz” instead of the number, and if the umber is divisible by 3 and 5 log “fizzbuzz” instead of the number.
function fizzBuzz (num){ for (i = 1; i <= num; i++){ if(i % 3 === 0 && i % 5 === 0){ consol.log("fizzbuzz") }else if(i % 3 === 0){ consol.log("fizz") }else if(i % 5 === 0){ consol.log("buzz") }else{ console.log(i) }
}
}
fizzBuzz(10)
How to find the average of number present in an array?
let sum = 0
for( let i = 0; i < numbers.lenght; i++){ sum = sum + nums[i] } consol.log(sum / numbers.lenght)
create a program that will take two lists of integers, a and b. Each list will consist of 3 positive integers above 0, representing the dimensions of cuboids a and b. You must find the difference of the cuboids’ volumes regardless of which is bigger.
function findDifference(a, b) { let cuboidOne = a.reduce((acc, c) => acc * c, 1) let cuboidTwo = b.reduce((acc, c) => acc * c, 1) let diff = cuboidOne - cuboidTwo return(Math.abs(diff)) }
(this is not the best of solving it)
Your function takes two arguments:
- current father’s age (years)
- current age of his son (years)
Сalculate how many years ago the father was twice as old as his son (or in how many years he will be twice as old).
function twiceAsOld(dadYearsOld, sonYearsOld) { let difference = dadYearsOld - sonYearsOld if(difference > sonYearsOld){ return(difference - sonYearsOld) }else if(difference < sonYearsOld){ return(sonYearsOld - difference) }else{ return(0) } }
Write a function that takes a list and an integer and returns a list with up to the n-th element in the list. In case of the integer being bigger than the size of the list return the original list.
function take(arr, number) { return arr.slice(0, number); }
Complete the function that takes a non-negative integer n as input, and returns a list of all the powers of 2 with the exponent ranging from 0 to n ( inclusive ).
Examples
n = 0 ==> [1] # [2^0]
n = 1 ==> [1, 2] # [2^0, 2^1]
n = 2 ==> [1, 2, 4] # [2^0, 2^1, 2^2]
function powersOfTwo(n){ var result = []; for (var i = 0; i <= n; i++) { result.push(Math.pow(2, i)); } return result; }
Timmy & Sarah think they are in love, but around where they live, they will only know once they pick a flower each. If one of the flowers has an even number of petals and the other has an odd number of petals it means they are in love.
Write a function that will take the number of petals of each flower and return true if they are in love and false if they aren’t.
function lovefunc(flower1, flower2){ return flower1 % 2 !== flower2 % 2; }
Return the number (count) of vowels in the given string.
We will consider a, e, i, o, u as vowels for this Kata (but not y).
The input string will only consist of lower case letters and/or spaces.
function getCount(str) { var vowelsCount = 0; str.split("").forEach(function(x){ if(x == "a" | x == "e" | x == "i" | x == "o" | x == "u"){ vowelsCount += 1; } }); return vowelsCount; }
Given two integer arrays a, b of lenght >= 1, create a programe that returns true if the sum of the squares of each element in ‘a’ is greater than the sum of the cubes of each element in ‘b’
function compareSquareAndCube (a,b){ return a.reduce((acc,c) => acc + c**2, 0) > b.reduce((acc,c) => acc + c**3 ,0) }
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.
function countBy(x, n) { let z = []; for(let i = x; i <=(x*n); i+=x){ z.push(i); } return z; }