HackerRank.com — 10 Days of JavaScript — Solutions
Here is my solutions of HackerRank — 10 Days of JavaScript Practice problem ,.
Note : I’m just attaching the solutions part only
Hence Day 0 is nothing hard that’s why I’m Not writing for Day 0
Day 1: Arithmetic Operators
Problem : https://www.hackerrank.com/challenges/js10-arithmetic-operators/problem
Solutions :
function getArea(length, width) { let area;
// Write your code here area = length * width;
return area;}
Day 1: Functions
Problem : https://www.hackerrank.com/challenges/js10-function/problem
Solutions :
function factorial(n) {
let res = 1;
for(let i = 2; i<=n; i++) {
res *= i;
}
return res;}
Day 1: Let and Const
Problem : https://www.hackerrank.com/challenges/js10-let-and-const/problem
Solution :
let r = readLine();const PI = Math.PI;console.log(PI * (r**2));console.log(2 * (PI*r));
Day 2: Conditional Statements: If-Else
Problem : https://www.hackerrank.com/challenges/js10-if-else/problem
Solution :
function getGrade(score) { let grade; if(score > 25 && score <=30){
grade = 'A'
}else if(score > 20 && score <=25){
grade = 'B'
}else if(score > 15 && score <= 20) {
grade = 'C'
}else if(score > 10 && score <=15){
grade = 'D'
}else if(score > 5 && score <=15){
grade = 'E'
}else if(score >=0 && score <= 5) {
grade = 'F'
}
return grade;}
Day 2: Conditional Statements: Switch
Problem : https://www.hackerrank.com/challenges/js10-switch/problem
Solutions :
function getLetter(s) {
let letter; // Write your code here
switch(true) {
case 'aeiou'.includes(s[0]):
letter = 'A';
break; case 'bcdfg'.includes(s[0]):
letter = 'B';
break; case 'hjklm'.includes(s[0]):
letter = 'C';
break; case 'npqrstvwxyz'.includes(s[0]):
letter = 'D';
break;
} return letter;}
Day 2: Loops
Problem : https://www.hackerrank.com/challenges/js10-loops/problem
Solutions :
function vowelsAndConsonants(s) {
for(let i=0; i<s.length; i++) {
let c = s[i] if(c==='a' || c==='e' || c==='i' || c==='o' || c==='u'){
console.log(c)
} } for(let i=0; i<s.length; i++) {
let c = s[i] if(c !== 'a' && c!=='e' && c!=='i' && c!=='o' && c!=='u') {
console.log(c)
} }}
Day 3: Arrays
Problem : https://www.hackerrank.com/challenges/js10-arrays/problem
Solutions :
function getSecondLargest(nums) {
let max1 = 0; for (const e of nums) {
if (max1 < e) {
max1 = e;
}
} let max2 = 0;
for (const e of nums) {
if (max2 < e && e < max1) {
max2 = e;
}
} return max2;
}
Day 3: Try, Catch, and Finally
Problem : https://www.hackerrank.com/challenges/js10-try-catch-and-finally/problem
Solutions :
function reverseString(s) { try {
s = s.split('').reverse().join('');
}catch(e) {
console.log(e.message);
}finally {
console.log(s);
}}
Day 3: Throw
Problem : https://www.hackerrank.com/challenges/js10-throw/problem
Solution :
function isPositive(a) { if(a > 0){
return 'YES'
}else if(a===0){
throw new Error("Zero Error")
}else{
throw new Error("Negative Error")
}}
Day 4: Count Objects
Problem : https://www.hackerrank.com/challenges/js10-count-objects/problem
Solutions :
function getCount(objects) { return objects.filter(ob => ob.x === ob.y).length}
Day 4: Classes
Problem :
Solution: https://www.hackerrank.com/challenges/js10-class/problem
class Polygon{
constructor(arr){
this.sides = arr;
} perimeter(){
return this.sides.reduce((acc,crr) => acc = acc + crr , 0)
}}
Day 5: Inheritance
Problem : https://www.hackerrank.com/challenges/js10-inheritance/problem
Solutions :
Rectangle.prototype.area = function() {
return this.w * this.h
}class Square extends Rectangle{
constructor(s){
super(s, s);
this.w = s;
this.h = s;
}}
Day 5: Template Literals
Problem : https://www.hackerrank.com/challenges/js10-template-literals/problem
Solutions :
function sides(literals, ...expressions) {
const [a, p] = expressions;
let s1 = (p + Math.sqrt(p**2 -16 *a))/4;
let s2 = (p - Math.sqrt(p**2 -16 *a))/4;
return [s1, s2].sort();
}
Day 5: Arrow Functions
Problem : https://www.hackerrank.com/challenges/js10-arrows/problem
Solution :
function modifyArray(nums) {
return nums.map(num => num % 2 === 0 ? num*2 : num*3);
}
Day 6: Bitwise Operators
Problem : https://www.hackerrank.com/challenges/js10-bitwise/problem
Solution :
function getMaxLessThanK(n, k) {
let max_anb = 0;
for (let b = n; b > 0; b--) {
for (let a = b-1; a > 0; a--) {
if ((a & b) < k && (a & b) > max_anb){
max_anb = (a&b);
}
}
}
return max_anb;
}
Day 7: Regular Expressions I
Problem : https://www.hackerrank.com/challenges/js10-regexp-1/problem
Solution :
function regexVar() {
let re = /^([aeiou]).+\1$/; return re;
}