CodeWars Flashcards

1
Q

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.

A
function enough(cap, on, wait) {
  if (on + wait < cap){
  return 0;
  } else {
  return (on + wait) - cap;
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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 :)

A
hero = (bullets, dragons) =>{
if (bullets/2 >= dragons) {
  return true;
}
else {
  return false;
}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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.

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to find the average of number present in an array?

A

let sum = 0

for( let i = 0; i < numbers.lenght; i++){
    sum = sum + nums[i]
}
consol.log(sum / numbers.lenght)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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.

A
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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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).

A
function twiceAsOld(dadYearsOld, sonYearsOld) {
 let difference = dadYearsOld - sonYearsOld
  if(difference > sonYearsOld){
   return(difference - sonYearsOld)
 }else if(difference < sonYearsOld){
   return(sonYearsOld - difference)
 }else{
   return(0)
 }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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.

A
function take(arr, number) {
  return arr.slice(0, number);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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]

A
function powersOfTwo(n){
  var result = [];
  for (var i = 0; i <= n; i++) {
    result.push(Math.pow(2, i));
  }
  return result;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.

A
function lovefunc(flower1, flower2){
  return flower1 % 2 !== flower2 % 2;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A
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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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’

A
function compareSquareAndCube (a,b){
    return a.reduce((acc,c) => acc + c**2, 0) > b.reduce((acc,c) => acc + c**3 ,0)
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.

A
function countBy(x, n) {
    let z = [];
    for(let i = x; i <=(x*n); i+=x){
      z.push(i);
    }
      return z;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly