Last active
May 11, 2024 11:58
-
-
Save saloni-jain484/d85045c81fb6f8e9142d34596102085d to your computer and use it in GitHub Desktop.
Hacker Rank - 10 Days of JavaScript Practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Task | |
Complete the vowelsAndConsonants function in the editor below. It has one parameter, a string, , consisting of lowercase English alphabetic letters (i.e., a through z). The function must do the following: | |
First, print each vowel in 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 . | |
Second, print each consonant (i.e., non-vowel) in on a new line in the same order as it appeared in string. | |
Solution | |
function vowelsAndConsonants(s) { | |
const vowels = 'aeiou'; | |
var consonants = ''; | |
for(var i = 0; i < s.length; i++) { | |
if (vowels.includes(s[i])) { | |
console.log(s[i]); | |
} | |
else { | |
consonants += s[i] + '\n'; | |
} | |
} | |
console.log(consonants.trim()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Task | |
Implement a function named factorial that has one parameter: an integer, . It must return the value of n(i.e., factorial). | |
Solution | |
function factorial(n){ | |
var ans=1; | |
for(var i=1;i<=n;i++){ | |
ans=ans*i; | |
} | |
return ans; | |
} | |
function main() { | |
const n = +(readLine()); | |
console.log(factorial(n)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Day 1: Let and Const | |
Objective | |
In this challenge, we practice declaring variables using the let and const keywords. Check out the attached tutorial for more details. | |
Task | |
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). | |
Read a number, r, denoting the radius of a circle from stdin. | |
Use PI and r to calculate the area and perimeter of a circle having radius . | |
Print area as the first line of output and print perimeter as the second line of output. | |
Input Format | |
A single integer, r, denoting the radius of a circle. | |
Constraints | |
0 <= n <= 100 | |
r is a floating-point number scaled to at most 3 decimal places. | |
Solution | |
'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 main() { | |
const PI=Math.PI; | |
let r=parseFloat(readLine()); | |
console.log(PI*r*r); | |
console.log(2*PI*r); | |
try { | |
// Attempt to redefine the value of constant variable PI | |
PI = 0; | |
// Attempt to print the value of PI | |
console.log(PI); | |
} catch(error) { | |
console.error("You correctly declared 'PI' as a constant."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Complete the function in the editor below by returning a RegExp object, , that matches any string that begins and ends with the same vowel. Recall that the English vowels are a, e, i, o, and u. | |
Constraints | |
The length of string is s>=3. | |
String consists of lowercase letters only (i.e., [a-z]). | |
'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 regexVar() { | |
return new RegExp(/^([aeiou]).*\1$/); | |
return re; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Saloni,
I have been thinking in a way to solve this problem (regular Expression 1 - Day 7), and I could solve part of this problem. But after a while I found here your solution. Have you any way to explain to me how is that happens / works?
I cannot understand why a two returns function works in this:
function regexVar() {
return new RegExp(/^([aeiou]).*\1$/);
return re;
}