Solutions
HackerRank
HackerRank
  • Home
  • 👨🏻‍💻 Profile
  • Prepare
    • Linux Shell
      • Bash
    • Python
      • Introduction
    • SQL
      • Basic Select
      • Advanced Select
      • Aggregation
      • Basic Join
  • Tutorials
    • 10 Days of Javascript
      • Day 0
      • Day 1
      • Day 2
      • Day 3
  • Certify
    • C# (Basic)
    • JavaScript (Basic)
    • SQL (Basic)
    • Rest API (Intermediate)
Powered by GitBook
On this page
  • Conditional Statements: If-Else
  • Conditional Statements: Switch
  • Loops

Was this helpful?

  1. Tutorials
  2. 10 Days of Javascript

Day 2

Conditional Statements: If-Else, Switch, Loops

PreviousDay 1NextDay 3

Last updated 2 years ago

Was this helpful?

Conditional Statements: If-Else

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
        grade = "F";
        
    return grade;
}

Conditional Statements: Switch

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

Loops

/*
 * Complete the vowelsAndConsonants function.
 * Print your output using 'console.log()'.
 */
function vowelsAndConsonants(s) {
    const vowels = ["a","e","i","o","u"];
    const str = s.split("");
    const vowelsArr = [];
    const consonantsArr = [];
    for(let i = 0; i < str.length; i++)
        vowels.includes(str[i]) ? vowelsArr.push(str[i]) : consonantsArr.push(str[i]);
    for(let i = 0; i < vowelsArr.length; i++)
        console.log(vowelsArr[i]);
    for(let i = 0; i < consonantsArr.length; i++)
        console.log(consonantsArr[i]);
}
LogoDay 2: Loops | HackerRankHackerRank
LogoDay 2: Conditional Statements: Switch | HackerRankHackerRank
LogoDay 2: Conditional Statements: If-Else | HackerRankHackerRank