10 Days Of Javascript Hackerrank Solution

Day:0 Hello, World!

My Solution

function greeting(parameterVariable) {
// This line prints 'Hello, World!' to the console:
//console.log('Hello, World!');
// Write a line of code that prints parameterVariable to
//stdout using console.log:

console.log(parameterVariable); //This is all you have to do
}


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(Number(firstInteger) + Number(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 + (+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);
}

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

/*
* Create the function factorial here
*/
function factorial(n){
if (n == 0){
return 1;
}
else{
return n * factorial(n-1);
}
}


function main() {
// Write your code here. Read input using 'readLine()' and print output using 'console.log()'.
let r = readLine();
var Pi = Math.PI;
let area, perimeter;


// Print the area of the circle:
area = Pi*(r*r);
console.log(area);


// Print the perimeter of the circle:
perimeter = 2*Pi*r;
console.log(perimeter);

}

function getGrade(score) {
let grade;
// Write your code here
if(score <= 30 && score> 25) return "A";
else if(score <= 25 && score> 20) grade = "B";
else if(score <= 20 && score> 15) return "C";
else if(score <= 15 && score> 10){
return "D";
}
else if(score > 5 && score <= 10){ grade='E' ; } else if(score <=5 && score>
= 0) return "F";
else return "Invalid Number";
return grade;
}


function getLetter(s) {
let letter;
// Write your code here
switch(true){
case 'aeiou'.includes(s[0]) :
return "A";
break;
case 'bcdfg'.includes(s[0]) :
return "B";
break;
case 'hjklm'.includes(s[0]) :
return "C";
break;
case 'npqrstvwxyz'.includes(s[0]) :
return "D";
break;
default:
return "Invalid string";
break;
}
return letter;
}

OR

function getLetter(s) {
let letter;
// Write your code here
switch(s[0].toLowerCase()){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return "A";
break;
case 'b':
case 'c':
case 'd':
case 'f':
case 'g':
return "B";
break;
case 'h':
case 'j':
case 'k':
case 'l':
case 'm':
return "C";
break;
default:
return "D";
break;
}
return letter;
}

/*
* Complete the vowelsAndConsonants function.
* Print your output using 'console.log()'.
*/
function vowelsAndConsonants(s) {
const vowel = 'aeiou';
var consonant = '';

for(var i=0; i < s.length; i++){
if(vowel.includes(s[i])){
console.log(s[i]);
} else consonant += s[i] + '\n';
} console.log(consonant.trim());
}

/** * 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 largest = nums[0];
let second_largest = 0;
for(const i of nums){
if(largest < i){
largest = i;
} } for(const i of nums){
if(second_largest < i && i < largest){
second_largest = i;
} } return second_largest;

}

/*
* Complete the reverseString function
* Use console.log() to print to stdout.
*/
function reverseString(s) {
try{
let rev_string = s.split('');
rev_string.reverse();
s = rev_string.join('');
console.log(s);
} catch(e){
console.log(e.message);
console.log(s);
}
}

OR

function reverseString(s) {
try{
let rev_string = s.split('');
rev_string.reverse();
s = rev_string.join('');
}
catch(e){
console.log(e.message);
}
finally{
console.log(s);
}
}

function isPositive(a) {
if(a > 0){
return "YES";
}
else if(a === 0){
throw Error("Zero Error");
}else throw Error ("Negative Error");
}

OR

function isPositive(a) {
if(a > 0){
return "YES";
} else{
throw Error(a ? "Negative Error" : "Zero Error");
}
}

/*
* Complete the Rectangle function
*/
function Rectangle(a, b) {
this.length = a;
this.width = b;
this.perimeter = 2*(a + b);
this.area = (a * b);
}

/*
* Return a count of the total number of objects 'o' satisfying o.x == o.y.
*
* Parameter(s):
* objects: an array of objects with integer properties 'x' and 'y'
*/
function getCount(objects) {
let count = 0;
for(let i = 0; i < objects.length; i++){
if(objects[i].x==objects[i].y){
count++;
}
}
return count;
}

/* * Implement a Polygon class with the following properties:
* 1. A constructor that takes an array of integer side lengths.
* 2. A 'perimeter' method that returns the sum of the Polygon's side lengths.
*/
class Polygon{
constructor(sides){
this.sides = sides;
}
perimeter(){
let polygon_parameter = 0; // perimeter is sum of all sides.

for(let i = 0; i < this.sides.length; i++){
polygon_parameter += this.sides[i];
}
return polygon_parameter;
}
}

/*
* Write code that adds an 'area' method to the Rectangle class' prototype
*/
Rectangle.prototype.area = function(){
return (this.w * this.h);
}
/*
* Create a Square class that inherits from Rectangle and implement its class constructor
*/
class Square extends Rectangle{
constructor(side){
super(side,side);
}
}

/*
* Determine the original side lengths and return an array:
* - The first element is the length of the shorter side
* - The second element is the length of the longer side
*
* Parameter(s):
* literals: The tagged template literal's array of strings.
* expressions: The tagged template literal's array of expression values (i.e., [area, perimeter]).
*/
function sides(literals, ...expressions) {
const [a, p] = expressions;
const root = Math.sqrt((p*p)-(16*a)) //formula
const s1 = (p + root)/4;
const s2 = (p - root)/4;
return ([s2, s1]).sort();

}

/*
* Modify and return the array so that all even elements are doubled and all odd elements are tripled.
*
* Parameter(s):
* nums: An array of numbers.
*/

Simple and Basic Approach

function modifyArray(nums) {
let newArray = [];

for(let i of nums){
if(i%2 == 0) newArray.push(i*2);
else newArray.push(i*3);
}
return newArray;
}

Using Arrow function

function modifyArray(nums) {
var newArray = nums.map(i => {
if(i%2==0) return i*2;
else return i*3;
});
return newArray;
}

One-liner Using Arrow function

function modifyArray(nums) {
return nums.map(i => i = (i%2==0) ? i*2 : i*3); }

function getMaxLessThanK(n, k) {
let max_AandB = 0;
for (let b = n; b > 0; b--) {
for (let a = b-1; a > 0; a--) {
if ((a & b) < k && (a & b)> max_AandB){
max_AandB = (a&b);
}
}
}
return max_AandB;
}

*Note: In this question, I take help from discussion tab, because i am not able to understand the question language.

// The days of the week are: "Sunday", "Monday", "Tuesday",
//"Wednesday", "Thursday", "Friday", "Saturday"

Simple and Basic Approach

function getDayName(dateString) {
let dayName;
// Write your code here
dayName = new Date(dateString).toString().slice(0, 3);
switch (dayName) {
case 'Sun':
dayName = 'Sunday';
break;
case 'Mon':
dayName = 'Monday';
break;
case 'Tue':
dayName = 'Tuesday';
break;
case 'Wed':
dayName = 'Wednesday';
break;
case 'Thu':
dayName = 'Thursday';
break;
case 'Fri':
dayName = 'Friday';
break;
case 'Sat':
dayName = 'Saturday';
break;
}
return dayName;
}

Another approach

function getDayName(dateString) {
const date = new Date(dateString);

const options = {
weekday: 'long'
};

return new Intl.DateTimeFormat('en-Us', options).format(date);
} *Note: Another approach is the approach of a person named "@tugay_yaldiz", I found it from Discussion tab and very helpful.

function regexVar() {
/*
* Declare a RegExp object variable named 're'
* It must match a string that starts and ends with the same vowel
(i.e., {a, e, i, o, u}) */
const re = new RegExp('^([aeiou]).*\\1$'); /*
* Do not remove the return statement
*/
return re;
}

function regexVar() {
/*
* Declare a RegExp object variable named 're'
* It must match a string that starts with 'Mr.', 'Mrs.', 'Ms.', 'Dr.',
* or 'Er.',
* followed by one or more letters.
*/
const re = new RegExp('^(Mr|Mrs|Ms|Dr|Er)(\\.)([a-zA-Z])+$') /*
* Do not remove the return statement
*/
return re;
}

function regexVar() {
/*
* Declare a RegExp object variable named 're'
* It must match ALL occurrences of numbers in a string.
*/

let re = /\d+/g;
/*
* Do not remove the return statement
*/
return re;
}

function regexVar() {
/*
* Declare a RegExp object variable named 're'
* It must match ALL occurrences of numbers in a string.
*/

let re = /\d+/g;
/*
* Do not remove the return statement
*/
return re;
}

index.html

!-- Enter your HTML code here -->
link rel="stylesheet" href="css/button.css" type="text/css">
script src="js/button.js" type="text/javascript"> /script>

button.css

#btn {
width: 96px;
height: 48px;
font-size: 24px;
}

button.js

var btn = document.createElement("Button");

btn.innerHTML = "0";
btn.id = "btn";
document.body.appendChild(btn);

btn.onclick = function() {
btn.innerHTML++;
}

button.css

#btns {
width: 75%;
position: relative;
}
.btn {
width: 30%;
height: 48px;
font-size: 24px;
}

button.js

let btn1 = document.getElementById("btn1");
let btn2 = document.getElementById("btn2");
let btn3 = document.getElementById("btn3");
let btn4 = document.getElementById("btn4");
let btn5 = document.getElementById("btn5");
let btn6 = document.getElementById("btn6");
let btn7 = document.getElementById("btn7");
let btn8 = document.getElementById("btn8");
let btn9 = document.getElementById("btn9");

function clockwiseRotation() {
[btn1.innerHTML,
btn2.innerHTML,
btn3.innerHTML,
btn4.innerHTML,
btn6.innerHTML,
btn7.innerHTML,
btn8.innerHTML,
btn9.innerHTML] =
        [btn4.innerHTML,
        btn1.innerHTML,
        btn2.innerHTML,
        btn7.innerHTML,
        btn3.innerHTML,
        btn8.innerHTML,
        btn9.innerHTML,
        btn6.innerHTML]
}
btn5.addEventListener("click", clockwiseRotation)

*Note: This is a kind request for everyone who takes help from this repo that this problem is a real time project type problem so please solve it by your own and judge yourself.