有人可以回答这个问题吗?它的Javascript

问题描述 投票:0回答:1
function getAverage(scores) {
  let sum = 0;

  for (const score of scores) {
    sum += score;
  }

  return sum / scores.length;
}
function getGrade(score) {
  if (score === 100) {
    return "A++";
  } else if (score >= 90) {
    return "A";
  } else if (score >= 80) {
    return "B";
  } else if (score >= 70) {
    return "C";
  } else if (score >= 60) {
    return "D";
  } else {
    return "F";
  }
}

function hasPassingGrade(score) {
  return getGrade(score) !== "F";
}

问题: 以totalScores和studentScore为参数完成studentMsg函数。该函数应返回一个字符串,表示给学生的消息。

如果学生通过了课程,则字符串应遵循以下格式:

示例代码 班级平均数:此处为平均数。您的成绩:成绩在此处。你通过了课程。 如果学生未通过课程,则字符串应遵循以下格式:

示例代码 班级平均数:此处为平均数。您的成绩:成绩在此处。你这门课不及格。 将此处的average-goes-here 替换为总分的平均值。将此处的grade-goes-here 替换为学生的成绩。

function studentMsg(totalScores, studentScore) {  
   if (hasPassingGrade==="F"){
    console.log ("Class average:+getAverage(totalScores).+Your grade:+getGrade(studentScore).+You failed the course.")
  }else{
    console.log("Class average:+getAverage(totalScores).+Your grade:+getGrade(studentScore).+You passed the course.")
    
  }
} 
console.log(studentMsg([92, 88, 12, 77, 57, 100, 67, 38, 97, 89], 37));

javascript function message
1个回答
-1
投票

在这里,您要寻找的是模板文字

通过使用模板文字,您可以在单行或多行中平滑地组合字符串和变量。

您在上面的示例中尝试过串联,但应该是这样的。

if (hasPassingGrade === "F") {
  console.log("Class average: " + getAverage(totalScores) +". Your grade: " + getGrade(studentScore) + ". You failed the course.")
} else {
  console.log("Class average: " + getAverage(totalScores) +". Your grade: " + getGrade(studentScore) +".  You passed the course.")
}

使用模板文字它应该看起来像这样。

function getAverage(scores) {
  let sum = 0

  for (const score of scores) {
    sum += score
  }

  return sum / scores.length
}

function getGrade(score) {
  if (score === 100) {
    return "A++"
  } else if (score >= 90) {
    return "A"
  } else if (score >= 80) {
    return "B"
  } else if (score >= 70) {
    return "C"
  } else if (score >= 60) {
    return "D"
  } else {
    return "F"
  }
}

function hasPassingGrade(score) {
  return getGrade(score) !== "F"
}

function studentMsg(totalScores, studentScore) {
  if (hasPassingGrade === "F") {
    console.log(
      `Class average: ${getAverage(totalScores)}. Your grade: ${getGrade(studentScore)}. You failed the course.`,
    )
  } else {
    console.log(
      `Class average: ${getAverage(totalScores)}. Your grade:${getGrade(studentScore)}. You passed the course.`,
    )
  }
}

console.log(studentMsg([92, 88, 12, 77, 57, 100, 67, 38, 97, 89], 37))

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.