HackerRank 10 Days of Javascript Problem solution
#01: Day 0: Hello, World!
In this problem simple just add one variable. Example below: →
function greeting(parameterVariable) {// This line prints 'Hello, World!' to the console:console.log('Hello, World!\n'+parameterVariable);}
#02: Day 0: Data Types
Sample Input 0
12
4.32
is the best place to learn and practice coding!
Sample Output 0
16
8.32
HackerRank is the best place to learn and practice coding!
Explanation 0
When we sum the integers and, we get the integer.
When we sum the floating-point numbers and, we get. When we concatenate HackerRank
with is the best place to learn and practice coding!
, we get HackerRank is the best place to learn and practice coding!
.
You will not pass this challenge if you attempt to assign the Sample Case values to your variables instead of following the instructions above.
The solution is: →
function performOperation(secondInteger, secondDecimal, secondString) {// Declare a variable named 'firstInteger' and initialize with integer value 4.const firstInteger = 4;// Declare a variable named 'firstDecimal' and initialize with floating-point value 4.0.const firstDecimal = 4.0;// Declare a variable named 'firstString' and initialize with the string "HackerRank".const firstString = 'HackerRank ';// Write code that uses console.log to print the sum of the 'firstInteger' and 'secondInteger' (converted to a Number type) on a new line.console.log(firstInteger+parseInt(secondInteger));// Write code that uses console.log to print the sum of 'firstDecimal' and 'secondDecimal' (converted to a Number type) on a new line.console.log(firstDecimal+parseFloat(secondDecimal));// Write code that uses console.log to print the concatenation of 'firstString' and 'secondString' on a new line. The variable 'firstString' must be printed first.console.log(firstString+secondString);}
#03: Day 1: Arithmetic Operators
Sample Input 0
3
4.5
Sample Output 0
13.5
15
Explanation 0
The area of the rectangle is length*width = 3*4.5 =13.5
The perimeter of the rectangle is. 2*(length+width)=2*(3+4.5)
The solution is:
function getArea(length, width) {let area;// Write your code herearea = length*width;return area;
}function getPerimeter(length, width) {let perimeter;// Write your code hereperimeter = 2*(length+width);return perimeter;
}
#04: Day 1: Functions
Sample Input 0
4
Sample Output 0
24
Explanation 0
We return the value of. 4! =4*3*2*1 = 24
The solution is:
/** Create the function factorial here*/function factorial(n){if(n>0 && n<=1){return n;}else{
return n * factorial(n-1);
}}
#05: Day 1: Let and Const
Sample Input 0
2.6
Sample Output 0
21.237166338267002
16.336281798666924
Explanation 0
Given the radius,r=2.5 we calculate the following:
area = pi*r² =21.2371
perimeter = 2*pi*r=16.3362
We then print area as our first line of output and perimeter as our second line of output.
The solution is:
// Write your code here. Read input using 'readLine()' and print output using 'console.log()'.const PI = Math.PI;const number = readLine();
// Print the area of the circle:console.log(PI*(number*number));// Print the perimeter of the circle:console.log(2*PI*number);
#06: Day 2: Conditional Statements: If-Else
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.
The solution is:
function getGrade(score) {let grade;// Write your code hereif(score>25 && score<=30){grade = 'A';}else if(score>20 && score<=25){grade ='B';}else if(score>15 && score<=20){grade ='C';}else if(score>10 && score<=15){grade ='D';}else if(score>5 && score<=10){grade ='E';}else if(score>0 && score<=5){grade ='F';}return grade;}
#07: Day 2: Conditional Statements: Switch
Sample Input 0
adfgt
Sample Output 0
A
Explanation 0
The first character of string s =adfgt is a
. Because the given criteria stipulate that we print A
any time the first character is in {a,e,i,o,u}, we return A
as our answer.
The solution is:
function getLetter(s) {let letter;// Write your code hereconst string = [...s];const firstChar = string[0];switch(firstChar){case 'a'||'e'||'i'||'o'||'u':letter='A';break;case 'b'||'c'||'d'||'f'||'g':letter='B';break;case 'h'||'j'||'k'||'l'||'m':letter='C';break;default:letter='D';}return letter;}
#08: Day 2: Loops
Sample Input 0
javascriptloops
Sample Output 0
a
a
i
o
o
j
v
s
c
r
p
t
l
p
s
Explanation 0
Observe the following:
- Each letter is printed on a new line.
- Then the vowels are printed in the same order as they appeared in s.
- Then the consonants are printed in the same order as they appeared in s.
The solution is:
function vowelsAndConsonants(s) {const array = [...s];const vowel = [];const consonant = [];array.forEach(word=>{if(word==='a' || word==='e' || word==='i' || word==='o' || word==='u'){
vowel.push(word);
}else{
consonant.push(word);
}})const finalWords =[...vowel,...consonant];
finalWords.map(alphabate=>{
console.log(alphabate);})}
#09: Day 3: Arrays
Sample Input 0
5
2 3 6 6 5
Sample Output 0
5
Explanation 0
Given the array, nums=[2,3,6,6,5] we see that the largest value in the array is 6 and the second largest value is 5. Thus, we return 5 as our answer.
function getSecondLargest(nums) {// Complete the functionlet largest = nums[0];let secondLargest = nums[0];for (let i = 1; i < nums.length; i++) {if (nums[i] > largest) {secondLargest = largest;largest = nums[i];continue;}if ((nums[i] > secondLargest) && (nums[i] < largest)) {secondLargest = nums[i];}}return secondLargest;}
#10: Day 3: Try, Catch, and Finally
The solution is:
function reverseString(s) {try{console.log(s.split("").reverse().join(""));}catch(err){console.log(err.message)console.log(s);}}
No more today. If you want to know more please search google and keep learning. If I make any mistakes please inform me. I will try my best to solve it. Thank you all.