DEV Community

Kevin Kim
Kevin Kim

Posted on • Updated on

JS Solutions for CodeWars Katas

In this article I will be documenting the solutions for codewars challenge questions that I am able to solve:

1.Your task is to sum the differences between consecutive pairs in the array in descending order.

For example:

[2, 1, 10] -> 9
In descending order: [10, 2, 1]

Sum: (10 - 2) + (2 - 1) = 8 + 1 = 9

If the array is empty or the array has only one element the result should be 0 ( Nothing in Haskell ).

My solution:

function sumOfDifferences(arr) {
  arr.sort(function(a, b){return b - a});
  let diff = 0
  for(let i=0;i<=arr.length - 2;i++){
    diff = diff + (arr[i] - arr[i + 1])
  }
  return diff
}
Enter fullscreen mode Exit fullscreen mode

2.A square of squares
You like building blocks. You especially like building blocks that are squares. And what you even like more, is to arrange them into a square of square building blocks!

However, sometimes, you can't arrange them into a square. Instead, you end up with an ordinary rectangle! Those blasted things! If you just had a way to know, whether you're currently working in vain… Wait! That's it! You just have to check if your number of building blocks is a perfect square.

Task
Given an integral number, determine if it's a square number:

In mathematics, a square number or perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself.

The tests will always use some integral number, so don't worry about that in dynamic typed languages.

Examples
-1 => false
0 => true
3 => false
4 => true
25 => true
26 => false

My solution:

var isSquare = function(n){
  if(n>-1){
    if(JSON.stringify(Math.sqrt(n)).includes('.')){
      return false
    }
    else{
      return true
    }
  }
  else{
    return false
  }
}
Enter fullscreen mode Exit fullscreen mode

Refactored solution:

var isSquare = n => Math.sqrt(n) % 1 === 0;
Enter fullscreen mode Exit fullscreen mode

3.Implement a function that adds two numbers together and returns their sum in binary. The conversion can be done before, or after the addition.

The binary number returned should be a string.

Examples:(Input1, Input2 --> Output (explanation)))

1, 1 --> "10" (1 + 1 = 2 in decimal or 10 in binary)
5, 9 --> "1110" (5 + 9 = 14 in decimal or 1110 in binary)

My solution:

function addBinary(a,b){
  return (a+b).toString(2)
}
Enter fullscreen mode Exit fullscreen mode

4.Given an array of integers, find the one that appears an odd number of times.

There will always be only one integer that appears an odd number of times.

Examples
[7] should return 7, because it occurs 1 time (which is odd).
[0] should return 0, because it occurs 1 time (which is odd).
[1,1,2] should return 2, because it occurs 1 time (which is odd).
[0,1,0,1,0] should return 0, because it occurs 3 times (which is odd).
[1,2,2,3,3,3,4,3,3,3,2,2,1] should return 4, because it appears 1 time (which is odd).

My solution:

function findOdd(A) {
  const count = {};

  for (const element of A) {
    if (count[element]) {
      count[element] += 1;
    } else {
      count[element] = 1;
    }
  }

  for (const property in count) {
    if(count[property] % 2 !== 0){
      return parseInt(property)
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

5.Make a program that filters a list of strings and returns a list with only your friends name in it.

If a name has exactly 4 letters in it, you can be sure that it has to be a friend of yours! Otherwise, you can be sure he's not...

Ex: Input = ["Ryan", "Kieran", "Jason", "Yous"], Output = ["Ryan", "Yous"]

i.e.

friend ["Ryan", "Kieran", "Mark"] shouldBe ["Ryan", "Mark"]
Note: keep the original order of the names in the output.

My solution:

const friend = friends => friends.filter(friend => friend.length === 4)
Enter fullscreen mode Exit fullscreen mode

Discussion (0)