Day 1: Let and Const Solution
Hello Friends in this article i am gone to share Hackerrank 10 days of javascript solutions with you. | Day 1: Let and Const Solution.
Also visit this link:ย Day 1: Functions Solution
Day 1: Let and Const Solution
Objective
In this challenge, we practice declaring variables using theย letย andย constย keywords. Check out the attached tutorial for more details.
Task
- Declare aย constant variable,ย PI, and assign it the valueย Math.PI. You will not pass this challenge unless the variable is declared as a constant and namedย PIย (uppercase).
- Read a number,ย r, denoting the radius of a circle from stdin.
- Useย PIย andย rย to calculate theย areaย andย perimeterย of a circle having radiusย .
- Printย areaย as the first line of output and printย perimeterย as the second line of output.
Input Format
A single integer,ย r, denoting the radius of a circle.
Constraints
- 0 <ย rย <= 100
- rย ย is a floating-point number scaled toย at mostย 3ย decimal places.
Output Format
Print the following two lines:
- On the first line, print theย areaย of the circle having radiusย r.
- On the second line, print theย perimeterย of the circle having radiusย r.
Sample Input 0
2.6
Sample Output 0
21.237166338267002
16.336281798666924
Explanation 0
Given the radiusย rย = 2.6, we calculate the following:
- areaย = ฯ *ย r^2 = 21.237166338267002
- perimeterย = 2 * ฯ *ย rย = 16.336281798666924
We then printย areaย as our first line of output andย perimeterย as our second line of output.
Solution โ Day 1: Let and Const
'useย strict'; process.stdin.resume(); process.stdin.setEncoding('utf-8'); letย inputStringย =ย ''; letย currentLineย =ย 0; process.stdin.on('data',ย inputStdinย =>ย { ย ย ย ย inputStringย +=ย inputStdin; }); process.stdin.on('end',ย _ย =>ย { ย ย ย ย inputStringย =ย inputString.trim().split('\n').map(stringย =>ย { ย ย ย ย ย ย ย ย returnย string.trim(); ย ย ย ย }); ย ย ย ย ย ย ย ย main();ย ย ย ย }); functionย readLine()ย { ย ย ย ย returnย inputString[currentLine++]; } functionย main()ย { ย ย ย ย //ย Writeย yourย codeย here.ย Readย inputย usingย 'readLine()'ย andย printย outputย usingย 'console.log()'. ย ย ย ย varย rย =ย readLine(); ย ย ย ย //ย Printย theย areaย ofย theย circle: ย ย ย ย console.log(Math.PIย *ย rย *ย r); ย ย ย ย //ย Printย theย perimeterย ofย theย circle: ย ย ย ย console.log(2ย *ย Math.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."); ย ย ย ย } }