What needs to change in the last else statement of this function to return the correct string if parameter 'unit' isnt === to 'f' or 'c'?

huangapple go评论70阅读模式
英文:

What needs to change in the last else statement of this function to return the correct string if parameter 'unit' isnt === to 'f' or 'c'?

问题

function convertToTemperature(degrees, unit) {
  if (unit === 'f') {
    const conversion = `${Math.round((degrees - 32) * 5/9)} C`;
    return conversion;
  } else if (unit === 'c') {
    const conversion = `${Math.round((degrees * 9/5) + 32)} F`;
    return conversion;
  } else {
    const conversion = `'${unit}' is not a valid measurement of Temperature`;
    return conversion;
  }
}
console.log(convertToTemperature(383, 'x'));

在你提供的代码中,问题出在条件语句的最后一个分支。你应该使用 else 来捕获所有不是 'f' 或 'c' 的情况,而不是 else if (unit = '')。我已经将代码进行了修改,现在它会在 unit 不是 'f' 或 'c' 时输出 'x' is not a valid measurement of Temperature

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function convertToTemperature(degrees, unit) {
  if (unit === &#39;f&#39;) {
    const conversion = `${Math.round((degrees - 32) * 5/9)} C`;
    return conversion;
  } else if (unit === &#39;c&#39;) {
    const conversion = `${Math.round((degrees * 9/5) + 32)} F`;
    return conversion;
  }
  
  // This is the bold part referenced in the question
  else if (unit = &#39;&#39;) {
    const conversion = `&#39;${unit}&#39; is not a valid measurement of Temperature`;
    return conversion;
  }
}
console.log(convertToTemperature(383, &#39;x&#39;));

<!-- end snippet -->

Because I'm using parameter 'x' when logging to the console I want it to log the string I've bolded. What is wrong in this section that isn't running. I'm assuming it's the "unit = ''" portion, I just don't know what it should be equal to for all other values. I tried to leave it blank (else if{}) but that didn't run either.

This function will output the correct code if I assign the parameter 'unit' a value of 'f' or 'c', but when I try to console.log 'unit' set to any other sting value it isn't logging the intended string (&#39;${unit}&#39; is not a valid measurement of Temperature) and instead returns undefined. I'm a beginner so take it easy on me lol

答案1

得分: 2

以下是翻译好的部分:

"You could return early and for the last just return the unknown."

"您可以提前返回,最后只需返回未知。"
function convertToTemperature(degrees, unit) {
    if (unit === 'f') return `${Math.round((degrees - 32) * 5/9)} C`;
    if (unit === 'c') return `${Math.round((degrees * 9/5) + 32)} F`;
    return `'${unit}' is not a valid measurement of Temperature`;
}

console.log(convertToTemperature(383, 'x'));

希望这有所帮助。如果您有任何其他翻译需求,请随时提出。

英文:

You could return early and for the last just return the unknown.

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->

function convertToTemperature(degrees, unit) {
    if (unit === &#39;f&#39;) return `${Math.round((degrees - 32) * 5/9)} C`;
    if (unit === &#39;c&#39;) return `${Math.round((degrees * 9/5) + 32)} F`;
    return `&#39;${unit}&#39; is not a valid measurement of Temperature`;
}

console.log(convertToTemperature(383, &#39;x&#39;));

<!-- end snippet -->

答案2

得分: 0

Since the last one covers everything besides f and c, you can just use an else.

function convertToTemperature(degrees, unit) {
  if (unit === 'f') {
    const conversion = `${Math.round((degrees - 32) * 5/9)} C`;
    return conversion;
  } else if (unit === 'c') {
    const conversion = `${Math.round((degrees * 9/5) + 32)} F`;
    return conversion;
  } else {
    const conversion = `'${unit}' is not a valid measurement of Temperature`;
    return conversion;
  }
}
console.log(convertToTemperature(383, 'x'));

Or if you want, you can also use a **switch** with the last as **default**

function convertToTemperature(degrees, unit) {
  switch(unit){
      case "f": return `${Math.round((degrees - 32) * 5/9)} C`;
      case "c": return `${Math.round((degrees * 9/5) + 32)} F`;
      default: return `'${unit}' is not a valid measurement of Temperature`;
  }
}
console.log(convertToTemperature(383, 'x'));
英文:

Since the last one covers everything besides f and c, you can just use an else.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function convertToTemperature(degrees, unit) {
  if (unit === &#39;f&#39;) {
    const conversion = `${Math.round((degrees - 32) * 5/9)} C`;
    return conversion;
  } else if (unit === &#39;c&#39;) {
    const conversion = `${Math.round((degrees * 9/5) + 32)} F`;
    return conversion;
  } else {
    const conversion = `&#39;${unit}&#39; is not a valid measurement of Temperature`;
    return conversion;
  }
}
console.log(convertToTemperature(383, &#39;x&#39;));

<!-- end snippet -->

Or if you want, you can also use a switch with the last as default

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function convertToTemperature(degrees, unit) {
  switch(unit){
      case &quot;f&quot;: return `${Math.round((degrees - 32) * 5/9)} C`;
      case &quot;c&quot;: return `${Math.round((degrees * 9/5) + 32)} F`;
      default: return `&#39;${unit}&#39; is not a valid measurement of Temperature`;
  }
}
console.log(convertToTemperature(383, &#39;x&#39;));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月30日 05:05:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定