Posts

Showing posts from October, 2020

10 Days of JavaScript (Day 7) HackerRank Solutions #10daysofjavascript

 DAY 7: Regular Expressions I Solution >>> function regexVar() {   let re = /^([aeiou]).+\1$/;   return re; } DAY 7: Regular Expressions II Solution >>> function regexVar() {   let re = /^(Mr|Mrs|Ms|Dr|Er)(\.)([a-zA-Z])+$/;   return re; } DAY 7: Regular Expressions III Solution >>> function regexVar() {   let re = /\d+/g;   return re; }

10 Days of JavaScript (Day 6) HackerRank Solutions #10daysofjavascript

 DAY 6: Bitwise Operators Solution >>> function getMaxLessThanK(n, k) {   let max = 0;   for (let i = 1; i <= n; i++) {     for (let j = i + 1; j <= n; j++) {       (i & j) > max && (i & j) < k ? (max = i & j) : max;     }   }   return max; } DAY 6: JavaScript Dates Solution >>> function getDayName(dateString) {   let dayName;   const days = [     "Sunday",     "Monday",     "Tuesday",     "Wednesday",     "Thursday",     "Friday",     "Saturday"   ];   dayName = days[new Date(dateString).getUTCDay()];   return dayName; }

10 Days of JavaScript (Day 5) HackerRank Solutions #10daysofjavascript

 DAY 5: Inheritance Solution >>> Rectangle.prototype.area = function() {   return this.w * this.h; }; class Square extends Rectangle {   constructor(s) {     super(s);     this.h = s;     this.w = s;   } } DAY 5: Template Literals Solution >>> function sides(literals, ...expressions) {   const [area, perimeter] = expressions; // ES6 destructuring   const s1 = (perimeter + Math.sqrt(Math.pow(perimeter, 2) - 16 * area)) / 4;   const s2 = (perimeter - Math.sqrt(Math.pow(perimeter, 2) - 16 * area)) / 4;   return [s1, s2].sort(); } DAY 5: Arrow Functions Solution >>> function modifyArray(nums) {   let newArr = [];   for (let i = 0; i < nums.length; i++) {     if (nums[i] % 2 === 0) {       newArr.push(nums[i] * 2);     } else {       newArr.push(nums[i] * 3);     }   }   return newArr; }

10 Days of JavaScript (Day 4) HackerRank Solutions #10daysofjavascript

  DAY 4: Create a Rectangle Object Solution >>> function Rectangle(a, b) {   this.length = a;   this.width = b;   this.perimeter = 2 * (a + b);   this.area = a * b; } DAY 4: Count Objects Solution >>> function getCount(objects) {   let pairCount = 0;   for (let i = 0; i < objects.length; i++) {     if (objects[i].x === objects[i].y) {       pairCount++;     }   }   return pairCount; } DAY 4: Classes Solution >>> function Polygon(shape) {   this.type = shape;   this.perimeter = getPerimeter; } function getPerimeter() {   return this.type.reduce((a, b) => a + b); }

10 Days of JavaScript (Day 3) HackerRank Solutions #10daysofjavascript

 DAY 3: Arrays Solution >>> function getSecondLargest(nums) {   let firstLargestNum = 0;   let secondLargestNum = 0;   for (let i = 0; i < nums.length; i++) {     if (nums[i] > firstLargestNum) {       secondLargestNum = firstLargestNum;       firstLargestNum = nums[i];     }     if (nums[i] > secondLargestNum && nums[i] < firstLargestNum) {       secondLargestNum = nums[i];     }   }   return secondLargestNum; } DAY 3: Try, Catch, and Finally Solution >>> function reverseString(s) {   if (typeof s === "string") {     console.log(       s         .split("")         .reverse()         .join("")     );   } else {     console.log("s.split is not a function" + "\n" + s);   } } DAY 3: Throw Solution >>> function isPositive(a) {   if (a === 0) {     throw Error("Zero Error");   }   if (a < 0) {     throw Error("Negative Error");   }   return "YES"; }

10 Days of JavaScript (Day 2) HackerRank Solutions #10daysofjavascript

 DAY 2: Conditional Statements: If-Else Solution >>> function getGrade(score) {   if (score > 25) {     return "A";   } else if (score > 20) {     return "B";   } else if (score > 20) {     return "B";   } else if (score > 15) {     return "C";   } else if (score > 10) {     return "D";   } else if (score > 5) {     return "E";   } else {     return "F";   } } DAY 2: Conditional Statements: Switch Solution >>> function getLetter(s) {   let letter;   switch (true) {     case "aeiou".includes(s[0]):       letter = "A";       break;     case "bcdefg".includes(s[0]):       letter = "B";       break;     case "hijklm".includes(s[0]):       letter = "C";       break;     case "nopqrstuvwxyz".includes(s[0]):       letter = "D";       break;   }   return letter; } DAY 2: Loops Solution >>> function vow

10 Days of JavaScript (Day 1) HackerRank Solution #10daysofjavascript

  DAY 1: Arithmetic Operators Solution >>> function getArea(length, width) {   let area;   area = length * width;   return area; } function getPerimeter(length, width) {   let perimeter;   perimeter = 2 * (length + width);   return perimeter; } DAY 1: Functions Solution >>> function factorial(n) {   return n ? n * factorial(n - 1) : 1; } DAY 1: Let and Const Solution >>> function main() {     let r = readLine();     const PI = Math.PI;     console.log(PI * r * r);     console.log(2 * PI * r); }

10 Days of JavaScript (Day 0) HackerRank Solution #10daysofjavascript

  DAY 0: Hello, World! Solution >>> function greeting(parameterVariable) {   console.log("Hello, World!");   console.log(parameterVariable); } DAY 0: Data Types Solution >>> function performOperation(secondInteger, secondDecimal, secondString) {   const firstInteger = 4;   const firstDecimal = 4.0;   const firstString = "HackerRank ";   console.log(firstInteger + Number(secondInteger));   const firstNum = Number(firstDecimal).toFixed(2);   const secondNum = Number(secondDecimal).toFixed(2);   const answer = Number(firstNum) + Number(secondNum);   console.log(answer);   console.log(firstString + secondString); }