10 Days of JavaScript (Day 2) HackerRank Solutions #10daysofjavascript
DAY 2: Conditional Statements: If-Else
Solution >>>
function getGrade(score) {
if (score > 25) {
return "A";
} else if (score > 20) {
return "B";
} else if (score > 20) {
return "B";
} else if (score > 15) {
return "C";
} else if (score > 10) {
return "D";
} else if (score > 5) {
return "E";
} else {
return "F";
}
}
DAY 2: Conditional Statements: Switch
Solution >>>
function getLetter(s) {
let letter;
switch (true) {
case "aeiou".includes(s[0]):
letter = "A";
break;
case "bcdefg".includes(s[0]):
letter = "B";
break;
case "hijklm".includes(s[0]):
letter = "C";
break;
case "nopqrstuvwxyz".includes(s[0]):
letter = "D";
break;
}
return letter;
}
DAY 2: Loops
Solution >>>
function vowelsAndConsonants(s) {
const vowels = ["a", "e", "i", "o", "u"];
const string = s.split("");
let vowelArr = [];
let consonantArr = [];
for (let i = 0; i < string.length; i++) {
vowels.includes(string[i])
? vowelArr.push(string[i])
: consonantArr.push(string[i]);
}
for (let i = 0; i < vowelArr.length; i++) {
console.log(vowelArr[i]);
}
for (let i = 0; i < consonantArr.length; i++) {
console.log(consonantArr[i]);
}
}
Comments
Post a Comment