DEV Community

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

Posted on • Edited on • Originally published at codeperfectplus.com

6 1

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

Check out 30 Days of Code| CodePerfectplus for All solutions from this series.

React ❤️ to encourage Author.

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top 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';
    ...
}

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay