简体   繁体   中英

How to add a printed message from console.log directly into the return function string in javascript?

// The challenge is to say if someone got 15 out of 20 questions, they would have 75% score, which is a C. // 15/20 is 75%...you got a C (75%), // 90 to 100, A, B 80-89, C 70-79, D 60-69, F 0-59

How do I add the letter grades into my function string return function call?

let studentGrade = function (score, total =100) {

    let totalGrade = (score / total)
    let totalPercent = (totalGrade * 100)

    if (score >=90 && score <=100) {
        console.log('You got an A!')
    }

    else if (score >=80 && score <=89) {
        console.log('You got an B!')
    }

    else if (score >=70 && score <=79) {
        console.log('You got an C!')
    }

    else if (score >=60 && score <=69) {
        console.log('You got a D!')
    }

    else if (score <=59 ) {
        console.log('You got an E!')
    }

    return (`You scored ${score} of a total of ${total} questions, that is ${totalPercent}%, which is a X`)

}

let studentOne = studentGrade (50, 100)
console.log(studentOne)

Add an unset variable at the beginning like let thisGrade; . Set thisGrade to A or whatever the grade is, in the if-else logic.

Then you can use template substitution to include ${thisGrade} in your return value.

You can also reduce repetition by having only one console.log statement after the termination of the if-else logic, which is also referring to thisGrade .

Assuming that you're only interested in the letter being returned you could structure your code something like this.

let studentGrade = (score, total = 100) => {
    const totalGrade = (score / total);
    const totalPercent = (totalGrade * 100);
    let grade;

    if (score >=90 && score <=100) {
        grade = "A";
    } else if (score >=80 && score <=89) {
        grade = "B";
    } else if (...) {
    ...
    } else {
        ...
    }

    console.log(`You got a ${grade}!`); // Could be grammatically incorrect but you could wrap logic to make this correct
    return grade; 
}

let studentOne = studentGrade(95, 100);
console.log(studentOne); // "A"

By doing this you have the option to remove the log statement from the studentGrade function entirely giving it the single responsibility of calculating the grade and not having to deal with the output.

Thank you, that helped!

let studentGrade = function (score, total) {
let totalPercent = (score / total) * 100
let letterGrade = ''

if (totalPercent >= 90) {
    letterGrade = 'A'
}
else if (totalPercent >=80) {
    letterGrade = 'B'
}
else if (totalPercent >=70) {
    letterGrade = 'C'   
}
else if (totalPercent >=60) {
    letterGrade = 'D'
}
else {
    letterGrade = 'F'
}

return `You scored ${score} of a total of ${total} questions, that is 
${totalPercent}%, which is a ${letterGrade}`

}

let studentOne = studentGrade (50, 100)
console.log(studentOne)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM