DEV Community

Cover image for Day 1 - 10DaysOfJavaScript
Deepak Raj
Deepak Raj

Posted on • Updated on • Originally published at codeperfectplus.com

Day 1 - 10DaysOfJavaScript

Day 1 - 10DaysOfJavaScript

Day 1: Arithmetic Operators

  • A length and width are sent to two different functions
  • getArea must return the area of the shape dimensions sent
  • getPerimeter must return the perimeter of the shape dimensions sent
  • Complete the functions
/**
 *   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;
}
Enter fullscreen mode Exit fullscreen mode

Day 1: Functions

  • A integer of value n is provided
  • 1 ≤ n ≤ 10
  • Output the factorial value of n (n!, 4! = 4 x 3 x 2 x 1 = 24)
/*
 * Create the function factorial here
 * Recursion: To call itself is called recursion.
 */
function factorial(n) {
    if (n === 0) {
        return 1;
    }
    return n * factorial(n - 1);
}
Enter fullscreen mode Exit fullscreen mode

Day 1: Let and Const

  • A float value r is provided for the radius
  • 0 < r ≤ 100
  • Print (console.log) the area of the circle (π x r²)
  • Print (console.log) the perimeter of the circle (2πr)
  • Do not overwrite the try and catch but make sure the code still works
function main() {
    // Write your code here. Read input using 'readLine()' and print output using 'console.log()'.
    let r = readLine();
    const PI = Math.PI;
    // Print the area of the circle:
    console.log(PI * (r * r) );
    // Print the perimeter of the circle:
    console.log(2 * PI * r);
}
Enter fullscreen mode Exit fullscreen mode

Check out 30 Days of Code| CodePerfectplus for All solutions from this series.

React ❤️ to encourage Author.

Top comments (9)

Collapse
 
pentacular profile image
pentacular

Consider not using unnecessary variables.

function getArea(length, width) {
    return length * width;
}

const is about the variable, not the value, so r should be const as you do not reassign it.

function main() {
    const r = readLine();
    ...
}
Collapse
 
codeperfectplus profile image
Deepak Raj • Edited

The Above Code are from HackerRank. Some of code are autogenerated. Changing in some code mislead.

Check more at HackerRank

Collapse
 
pentacular profile image
pentacular

Sounds like HackerRank is doing a bad job -- why are you posting them here?

Thread Thread
 
codeperfectplus profile image
Deepak Raj

I am learning JavaScript and it's for encourage Other as well.

Thread Thread
 
pentacular profile image
pentacular

Then I'm not sure why you're talking about HackerRank auto-generated code?

Thread Thread
 
codeperfectplus profile image
Deepak Raj • Edited

Some of code are autogenerated, you have to only write logic according to question and complete the challenge. It's like Leet code but has Boilerplate code for beginners.

You can find an Example here 👇👇

hackerrank.com/challenges/js10-try...

Happy Coding 😀

Thread Thread
 
pentacular profile image
pentacular

Yes, but it looks like the quality of the auto-generated code is fairly low.

Collapse
 
makampos profile image
Matheus de Campos • Edited

Man I think in a few weeks start to learning the bases of javascript to reforce what I knowk, some times I realize its miss something.

I choose starting with Freecodecamp, is that a good ideia?

Collapse
 
codeperfectplus profile image
Deepak Raj

Freecodecamp is also awesome but I prefer HackerRank And LeetCode.