DEV Community 👩‍💻👨‍💻

Cover image for Day 2 - 10DaysOfJavaScript
Deepak Raj
Deepak Raj

Posted on • Updated on

Day 2 - 10DaysOfJavaScript

Day 2 - 10DaysOfJavaScript

Day 2: Conditional Statements: If-Else

  • An integer value score is provided for a students test
  • 0 ≤ score ≤ 30
  • It must return the letter corresponding to grade
function getGrade(score) {
    let grade;
    // Write your code here
    if(score<=5) {
        grade="F";
    }else if(score<=10) {
        grade='E';
    }else if(score<=15) {
        grade='D'
    }else if(score<=20) {
        grade ='C'
    }else if(score<=25) {
        grade ='B'
    }else if(score<=30) {
        grade='A'
    }
    return grade;
}

Enter fullscreen mode Exit fullscreen mode

Day 2: Conditional Statements: Switch

  • A string is provided where its length is 1 ≤ s ≤ 100
  • Given the following legend, return the correct value based on the first letter
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;
}

Enter fullscreen mode Exit fullscreen mode

Day 2: Loops

  • Given a string of s of any length
  • Output, in order, the vowels of that string on each new line
  • Right after, output, in order, the consonants of that string on each new line
/*
 * Complete the vowelsAndConsonants function.
 * Print your output using 'console.log()'.
 */
function vowelsAndConsonants(s) {
    let vowels = ['a','e','i','o','u'];

    for(let v of s) {
        if(vowels.includes(v))
            console.log(v);
    }
    for(let v of s) {
        if(!vowels.includes(v))
            console.log(v);
    }
}
string = 'javascriptloops'
vowelsAndConsonants(string)
Enter fullscreen mode Exit fullscreen mode

More Articles by Author

Join for Weekly Updates.

React ❤️ to encourage Author.

Oldest comments (1)

Collapse
 
pentacular profile image
pentacular

It would be simpler without the grade variable.
You can just return the value directly.

I also suggest using more spaces and not writing if(x) which looks like a function call.

function getGrade (score) {
    if (score <= 5) {
        return "F";
    } else if (score <= 10) {
       ...
    }
}

Likewise here, although here we can also use better variable names.

function getLetter (string) {
    const letter = string[0];
    // Write your code here
    switch (true) {
        case 'aeiou'.includes(letter):
            return 'A';
        case 'bcdfg'.includes(letter):
            ...
    }
}

You already know that includes works on strings, so why stop now?
And let's use const if you're not going to reassign something.

function vowelsAndConsonants (string) {
    const vowels = 'aeiou';
    ...
}

This post blew up on DEV in 2020:

js visualized

🚀⚙️ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! 🥳

Happy coding!