Day 2: Conditional Statements: If-Else Solution
Hello Friends in this article i am gone to share Hackerrank 10 days of javascript solutions with you. | Day 2: Conditional Statements: If-Else Solution.
Also visit this link:ย Day 1: Let and Const Solution
Day 2: Conditional Statements: If-Else Solution
Objective
In this challenge, we learn aboutย if-elseย statements. Check out the attached tutorial for more details.
Task
Complete theย getGrade(score)ย function in the editor. It has one parameter: an integer,ย score, denoting the number of points Julia earned on an exam. It must return the letter corresponding to herย gradeย according to the following rules:
- Ifย 25 <ย scoreย <= 30, thenย gradeย = A.
- Ifย 20 <ย scoreย <= 25, thenย gradeย = B.
- Ifย 15 <ย scoreย <= 20, thenย gradeย = C.
- Ifย 10 <ย scoreย <= 15, thenย gradeย = D.
- Ifย 5 <ย scoreย <= 10, thenย gradeย = E.
- Ifย 0 <ย scoreย <= 5, thenย gradeย = F.
Input Format
Stub code in the editor reads a single integer denotingย scoreย from stdin and passes it to the function.
Constraints
- 0 <=ย scoreย <= 30
Output Format
The function must return the value ofย gradeย (i.e., the letter grade) that Julia earned on the exam.
Sample Input 0
11
Sample Output 0
D
Explanation 0
Becauseย scoreย = 11, it satisfies the conditionย 10 <ย scoreย <= 15ย (which corresponds toย D). Thus, we returnย Dย as our answer.
Solution โ Day 2: Conditional Statements: If-Else
'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ย getGrade(score)ย { ย ย ย ย //ย Writeย yourย codeย here ย ย ย ย if(scoreย >ย 25ย &&ย scoreย <=ย 30){ ย ย ย ย ย ย ย ย return("A"); ย ย ย ย }elseย if(scoreย >ย 20ย &&ย scoreย <=ย 25){ ย ย ย ย ย ย ย ย return("B"); ย ย ย ย }elseย if(scoreย >ย 15ย &&ย scoreย <=ย 20){ ย ย ย ย ย ย ย ย return("C"); ย ย ย ย }elseย if(scoreย >ย 10ย &&ย scoreย <=ย 15){ ย ย ย ย ย ย ย ย return("D"); ย ย ย ย }elseย if(scoreย >ย 5ย &&ย scoreย <=ย 10){ ย ย ย ย ย ย ย ย return("E"); ย ย ย ย }elseย if(scoreย >ย 0ย &&ย scoreย <=ย 5){ ย ย ย ย ย ย ย ย return("F"); ย ย ย ย } ย ย ย ย ย ย ย ย returnย grade; } functionย main()ย { ย ย ย ย constย scoreย =ย +(readLine()); ย ย ย ย ย ย ย ย console.log(getGrade(score)); }