# 10 Days of JavaScript HackerRank Challenges

## Solve 10 Days of JavaScript HackerRank Problems

Today we’ve solved several JavaScript coding interview problems from hackerrank.com. Here we’ve presented 10 of them.

## 1. Functions

Problem:
Implement a function named factorial that has one parameter: an integer, . It must return the value of (i.e., factorial).

Solution:

## 2. Let and Const

Problem:
1.
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).
2. Read a number, r, denoting the radius of a circle from stdin.
3. Use PI and r to calculate the area and perimeter of a circle having radius r.
4. Print area as the first line of output and print perimeter as the second line of output.

Solution:

## 3. Conditional Statements: If-Else

Problem:
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.

Solution:

## 4. Conditional Statements: Switch

Problem:
Complete the getLetter(s) function in the editor. It has one parameter: a string, s, consisting of lowercase English alphabetic letters (i.e., a through z). It must return A, B, C, or D depending on the following criteria:

• If the first character in string s is in the set {a, e, i, o, u}, then return A.
• If the first character in string s is in the set {b, c, d, f, g}, then return B.
• If the first character in string s is in the set {h, j, k, l, m}, then return C.
• If the first character in string s is in the set {n, p, q, r, s, t, v, w, x, y, z}, then return D.

Hint: You can get the letter at some index in using the syntax s[i] or s.charAt(i).

Solution:

## 5. Loops

Problem:
Complete the vowelsAndConsonants function in the editor below. It has one parameter, a string, s, consisting of lowercase English alphabetic letters (i.e., a through z). The function must do the following:

1. First, print each vowel in s on a new line. The English vowels are a, e, i, o, and u, and each vowel must be printed in the same order as it appeared in s.
2. Second, print each consonant (i.e., non-vowel) in s on a new line in the same order as it appeared in s.

Solution:

## 6. Arrays

Problem:
Complete the getSecondLargest function in the editor below. It has one parameter: an array, nums, of n numbers. The function must find and return the second largest number in nums.

Solution:

## 7. Try, Catch, and Finally

Problem:
Complete the reverseString function; it has one parameter, s. You must perform the following actions:

1. Try to reverse string s using the split, reverse, and join methods.
2. If an exception is thrown, catch it and print the contents of the exception’s messages on a new line.
3. Print on a new line. If no exception was thrown, then this should be the reversed string; if an exception was thrown, this should be the original string.

Solution:

## 7. Throw

Problem:
Complete the isPositive function below. It has one integer parameter, a. If the value of is positive, it must return the string YES. Otherwise, it must throw an Error according to the following rules:

• If is 0, throw an Error with message = Zero Error.
• If a is negative, throw an Error with message = Negative Error.

Solution:

Problem:

Solution:

## 9. Count Objects

Problem:
Complete the function in the editor. It has one parameter: an array, a, of objects. Each object in the array has two integer properties denoted by x and y. The function must return a count of all such objects o in array a that satisfy o.x == o.y.

Solution:

## 10. Classes

Problem:
Create a Polygon class that has the following properties:

• A constructor that takes an array of integer values describing the lengths of the polygon’s sides.
• A perimeter() method that returns the polygon’s perimeter.

Locked code in the editor tests the Polygon constructor and the perimeter method.

Note: The perimeter method must be lowercase and spelled correctly.

Solution: