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;

}

Comments

Popular posts from this blog

Introduction to the Internet of Things and Embedded Systems (Week 2)

The Arduino Platform and C Programming (Week 1)