toggle menu

My Blogs

Solving Code Wars Problem in Javascript (ES6) [6th Kyu]


In this article we are going to solve 2 Codewars' [6th kyu ] problems using Javascript (ES6) and  discuss approaches how problems can be solved .

Codewars is a great site for improving your problem solving skills and it's also great for discovering different approaches of other developers in solving each problems.

Problems at code wars are label by difficulty as kyu, [1st kyu] being the hardest and [8th kyu] being the easiest.


Let's now start with our first problem.


1. Matrix Addition

Codewars link: https://www.codewars.com/kata/matrix-addition/train/javascript

The problem is :


Write a function that accepts two square matrices (N x N two dimensional arrays), and return the sum of the two. Both matrices being passed into the function will be of size N x N (square), containing only integers.

How to sum two matrices:

Take each cell [n][m] from the first matrix, and add it with the same [n][m] cell from the second matrix. This will be cell [n][m] of the solution matrix.

Visualization:

|1 2 3| |2 2 1| |1+2 2+2 3+1| |3 4 4|
|3 2 1| + |3 2 3| = |3+3 2+2 1+3| = |6 4 4|
|1 1 1| |1 1 3| |1+1 1+1 1+3| |2 2 4|


The solution:

Non ES6 approach: 


function matrixAddition(a, b) {
var m = []; // Matrix var for the sum of a + b
for (var r = 0; r < a.length; r++) {
m.push([]); // adds 1d array into the array to make it 2d
for (var c = 0; c < a[r].length; c++) {
m[r].push(a[r][c] + b[r][c]); // sum of each cell in a and b matrix
}
}

return m;
}


We have created m as the variable that will hold the sum of cells in both a and b in same row and col.

And every row iteration we are adding an empty 1d array to m, so that we can store the sum of same cells for both a and b.


And would you believe the approach above can be reduced into a single line of code?


ES6 Approach: 


const matrixAddition = (a, b) => a.map((a1, r) => a1.map((a2, c) => a2 + b[r][c]));


In this approach we have used arrow function of Javascript, to omit the function() keyword, to make it more shorter.

Also using arrow function would omit the return keyword.

We have used map here to mutate the matrix's value, and we have another inner map since we are dealing with 2d  array here.

Same as our solution above we have  and c which denotes the row and column of the cells we are adding .



2. Find the unique number

Codewars link: https://www.codewars.com/kata/find-the-unique-number-1/train/javascript

The problem is :

There is an array with some numbers. All numbers are equal except for one. Try to find it!

findUniq([ 1, 1, 1, 2, 1, 1 ]) === 2
findUniq([ 0, 0, 0.55, 0, 0 ]) === 0.55

It's guaranteed that array contains more than 3 numbers.

The tests contain some very huge arrays, so think about performance.

This is the first kata in series:



The solution:

Non ES6 approach: 

function findUniq(arr) {
var uniqObj = {}; //Stores all unique elements in array

arr.forEach(function (a) {
if (uniqObj.hasOwnProperty(a)) { //checks if the element in array exist already in the object as key
uniqObj[a] = true; // modify its value to true if exists already, meaning its unique
} else {
uniqObj[a] = false; // otherwise initialize it to false
}
});

var keys = Object.keys(uniqObj); // Gets all the keys in the Object

for (var i = 0; i < keys.length; i++) {
if (!uniqObj[keys[i]]) { // checks if the property value if false, if it is then it is unique
return parseInt(keys[i]); // return the key, parsed into integer
}
}
}


uniqObj is a variable that will hold the unique elements in the array as key.


hasOwnProperty() is an Object method that returns a boolean value if the key already exist in the object or not, if it exists we are setting it to true meaning the it is not unique, otherwise initialize it to false.

 

Object.keys() gets all the keys inside the object needed to iterate into the object values to find false in it, meaning it is a unique number then return the parseInt value of that key.


ES6 Approach: 

const findUniq = (arr) => {
const obj = arr.reduce((acc, a) => {
acc[a] = acc.hasOwnProperty(a);
return acc;
}, {});
return arr.reduce((acc, a) => (!obj[a] ? +a : acc), 0);
}


Uses the same idea as we did above, but instead of declaring a var uniqObj and using forEach to accumulate its value, we used reduce() instead, as well as to finding which key is the only unique number.

Instead of using if in checking to the unique number, we used a ternary operator.

We also change parseInt(strNumber) into +strNumber, which is the shorthand for parsing int.



That ends our article in Solving 6th kyu Codewars problem using ES6.


Check out the 7th kyu version of this article: http://onecompileman.com/blogs/3



Author

MY Profile

Stephen G. Vinuya
Dec 07,2018



Leave a comment below...