英文:
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 === '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;
}
// This is the bold part referenced in the question
else if (unit = '') {
const conversion = `'${unit}' is not a valid measurement of Temperature`;
return conversion;
}
}
console.log(convertToTemperature(383, 'x'));
<!-- 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 ('${unit}' 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 === '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'));
<!-- 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 === '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'));
<!-- 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 "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'));
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论