Codewars: started

Md. Jamal Uddin
Oct 15, 2018 · 4 min read

01. Convert a Number to a String!

Instructions:

Examples:

numberToString(123); // returns '123';`   
numberToString(999); // returns '999';`

Sample test:

Test.expect(numberToString(67) === ‘67’);

Solutions:

// Basic
function numberToString(num) {
return '' + num;
}
// ES6+ version
const numberToString = num => `${num}`;

02. Keep up the hoop

Instructions:

Tag: FUNDAMENTALS

Test Cases:

Test.assertEquals(hoopCount(3),”Keep at it until you get it” ) 
Test.assertEquals(hoopCount(11),”Great, now move on to tricks” )

Solutions:

// Basic
function hoopCount (n) {
return n < 10 ?
“Keep at it until you get it” :
“Great, now move on to tricks”;
}
// ES6+ version
const strOne = `Keep at it until you get it`;
const strTwo = `Great, now move on to tricks`;

const hoopCount = n => n < 10 ? strOne : strTwo;

03. String repeat

Example:

repeatStr(6, "I") // "IIIIII" 
repeatStr(5, "Hello") // "HelloHelloHelloHelloHello"

Test Cases:

Solutions:

// Basic
function repeatStr (n, s) {
let newStr = '';
for(let i = 0; i < n; i++) {
newStr += s;
}

return newStr;
}
// Using Built in method
function repeatStr (n, s) {
return s.repeat(n);
}
// ES6+ version
const repeatStr = (n, s) => s.repeat(n);

04. Calculate average

Test Cases:

Test.assertEquals(find_average([1,1,1]), 1);
Test.assertEquals(find_average([1,2,3]), 2);

Solutions:

// Basic
function find_average(array) {
var sum = 0;
const length = array.length;
for (let i = 0; i < length; i++){
sum += array[i];
}
return sum/length;
}
// ES6+ version
const find_average = (array) => {
return array.reduce((a, b) => a + b) / array.length;
}

05. Pillars

Instructions:

TAGS:

Test Cases:

Solutions:

// Basic
function pillars(num_pill, dist, width) {
if(num_pill > 1) {
return (dist * 100 * (num_pill - 1)) + ((num_pill - 2) * width);
} else return 0;
}
//ES6+
const pillars = (num_pill, dist, width) => {
return num_pill > 1 ?
(num_pill - 1) * dist * 100 + (num_pill - 2) * width : 0;
}

06. Basic Mathematical Operations

Instructions:

basicOp('+', 4, 7)         // Output: 11
basicOp('-', 15, 18) // Output: -3
basicOp('*', 5, 5) // Output: 25
basicOp('/', 49, 7) // Output: 7

TAGS:

Test Cases:

Solutions:

function basicOp(operation, value1, value2) {
switch (operation) {
case '+': return value1 + value2;
case '-': return value1 - value2;
case '*': return value1 * value2;
case '/': return value1 / value2;
default: return 'Operation must be one of + - * /';
}
}

Md. Jamal Uddin

Written by

Life Long Learner | Programmer | JavaScript | React | Gatsby | https://github.com/jamal-pb95