简体   繁体   English

javascript 中返回的 function 字符串,如何直接从 console.log 中打印一条消息?

[英]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 // 挑战是说如果有人在 20 个问题中得到 15 个,他们将有 75% 的分数,即 C。 // 15/20 是 75%...你得到了 C (75%),// 90 至 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?如何将字母等级添加到我的 function 字符串返回 function 调用中?

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;在开头添加一个未设置的变量,如let thisGrade; . . Set thisGrade to A or whatever the grade is, in the if-else logic.if-else逻辑thisGrade设置为A或任何等级。

Then you can use template substitution to include ${thisGrade} in your return value.然后您可以使用模板替换将${thisGrade}包含在您的返回值中。

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 .您还可以通过在if-else逻辑终止后仅使用一个console.log语句来减少重复,该逻辑也引用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.通过这样做,您可以选择从 studentGrade function 中删除日志语句,完全赋予它计算成绩的单一责任,而不必处理 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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