JavaScript Problem Solving In HackerRank

Md Piash
4 min readMay 15, 2020

--

1. Why need problem solving?

Problem-solving gives us a mechanism for identifying these things, figuring out why they are broken and determining a course of action to fix them. Addressing risk: Humans have learned to identify trends and developed an awareness of cause-and-effect relationships in their environment.
Effective problem solving skills enable employees to analyze problems, identify problem severity and assess the impact of alternative solutions. Workplace training designed to develop problem solving skills helps employees to work more efficiently with co-workers, customers, partners and suppliers.

2. Day 0 Data Type solved problem

function performOperation(secondInteger, secondDecimal, secondString) {

// Declare a variable named ‘firstInteger’ and initialize with integer value 4.

const firstInteger = 4;

secondInteger = parseInt(secondInteger);

console.log(firstInteger+secondInteger);

// Declare a variable named ‘firstDecimal’ and initialize with floating-point value 4.0.

const firstDecimal = 4.0;

secondDecimal = parseFloat(secondDecimal);

console.log(firstDecimal+secondDecimal);

// Declare a variable named ‘firstString’ and initialize with the string “HackerRank”.

const firstString = ‘HackerRank ‘;

console.log(firstString+secondString);

// Write code that uses console.log to print the sum of the ‘firstInteger’ and ‘secondInteger’ (converted to a Number type) on a new line.

// Write code that uses console.log to print the sum of ‘firstDecimal’ and ‘secondDecimal’ (converted to a Number type) on a new line.

// 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.

}

3.Day 1 Arithmetic’s Operator solved problem

‘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++];

}

/**

* 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;

}

4.Day1 Function problem solved

/*

* Create the function factorial here

*/

function factorial(n){

if(n==0 || n==1){

return 1;

}

return factorial(n-1)*n;

}

function main() {

const n = +(readLine());

console.log(factorial(n));

}

5.Day 1 Let and Const Solved

function main() {

// Write your code here. Read input using ‘readLine()’ and print output using ‘console.log()’.

// Print the area of the circle:

const PI= Math.PI;

let r = readLine();

let area = PI * (r*r);

// Print the perimeter of the circle:

let perimeter = 2 * PI * r;

console.log(area);

console.log(perimeter);

6. Day 2 Loops Solved

‘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++];

}

/*

* Complete the vowelsAndConsonants function.

* Print your output using ‘console.log()’.

*/

function vowelsAndConsonants(s) {

for(var i=0;i<s.length;i++){

let vowel = s[i];

if(‘aeiou’.includes(vowel)){

console.log(vowel);

}

}

for(var j=0;j<s.length;j++){

let consonant = s[j];

if(!’aeiou’.includes(consonant)){

console.log(consonant);

}

}

}

7.Day2 Conditional Statement Solved

function getGrade(score) {

let grade;

// Write your code here

if ( 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;

}

8. Day 3 Array solved problem:

‘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++];

}

/**

* Return the second largest number in the array.

* @param {Number[]} nums — An array of numbers.

* @return {Number} The second largest number in the array.

**/

function getSecondLargest(nums) {

// Complete the function

let num1 = nums[0];

let num2;

for(var i=0;i<nums.length;i++){

let num3 = nums[i];

if(num3 > num1)

{

num2=num1;

num1=num3;

}

else if (num3 < num1 && num3 > num2)

{

num2=num3;

}

}

return num2;

}

9.Day 3 try, catch and finally problem solved

function reverseString(s) {

try{

s = s.split(‘’).reverse().join(‘’);

} catch(err){

console.log(err.message);

} finally {

console.log(s);

}

}

10.Day3 Throw problem solved

function isPositive(a) {
if(a>0){
return “YES”;
}

else if(a==0){
throw new Error(“Zero Error”);
}

else {
throw new Error(“Negative Error”);
}
}

--

--

No responses yet