Solutions
HackerRank
HackerRank
  • Home
  • 👨🏻‍💻 Profile
  • Prepare
    • Linux Shell
      • Bash
    • Python
      • Introduction
    • SQL
      • Basic Select
      • Advanced Select
      • Aggregation
      • Basic Join
  • Tutorials
    • 10 Days of Javascript
      • Day 0
      • Day 1
      • Day 2
      • Day 3
  • Certify
    • C# (Basic)
    • JavaScript (Basic)
    • SQL (Basic)
    • Rest API (Intermediate)
Powered by GitBook
On this page
  • Arithmetic Operators
  • Functions
  • Let and Const

Was this helpful?

  1. Tutorials
  2. 10 Days of Javascript

Day 1

Arithmetic Operators, Functions, Let and Const

PreviousDay 0NextDay 2

Last updated 2 years ago

Was this helpful?

Arithmetic Operators

/**
*   Calculate the area of a rectangle.
*
*   length: The length of the rectangle.
*   width: The width of the rectangle.
*   
*	Return a number denoting the rectangle's area.
**/
function getArea(length, width) {
    let area;
    // Write your code here
    area = length * width;
    return area;
}

/**
*   Calculate the perimeter of a rectangle.
*	
*   length: The length of the rectangle.
*   width: The width of the rectangle.
*   
*	Return a number denoting the perimeter of a rectangle.
**/
function getPerimeter(length, width) {
    let perimeter;
    // Write your code here
    perimeter = 2 * (length + width);
    return perimeter;
}

Functions

/*
 * Create the function factorial here
 */
function factorial(n){
    return n ? n * factorial(n - 1) : 1;
}

Let and Const

function main() {
    // Write your code here. Read input using 'readLine()' and print output using 'console.log()'.
    const PI = Math.PI;
    let r = readLine(); 
    // Print the area of the circle:
    console.log(PI * r * r);
    // Print the perimeter of the circle:
    console.log(2 * PI * r);
    try {    
        // Attempt to redefine the value of constant variable PI
        PI = 0;
        // Attempt to print the value of PI
        console.log(PI);
    } catch(error) {
        console.error("You correctly declared 'PI' as a constant.");
    }
}
LogoDay 1: Let and Const | HackerRankHackerRank
LogoDay 1: Functions | HackerRankHackerRank
LogoDay 1: Arithmetic Operators | HackerRankHackerRank