This is my code:
function convertFahrToCelsius(temp) {
if(typeof(temp) === "number" || typeof(temp) === "string"){
return ((Number(temp) - 32) * 5/9).toFixed(4);
} else {
return `${temp} is not a valid number but a/an ${typeof(temp)}`;
}
}
console.log(convertFahrToCelsius(0));
console.log(convertFahrToCelsius("0") );
console.log(convertFahrToCelsius([1,2,3]));
console.log(convertFahrToCelsius({temp: 0}));
This should be the output:
- convertFahrToCelsius(0) should return `-17.7778`
- convertFahrToCelsius("0") should return `-17.7778`
- convertFahrToCelsius([1,2,3]) should return `[1,2,3] is not a valid number but a/an array.`
- convertFahrToCelsius({temp: 0}) should return `{temp: 0} is not a valid number but a/an object.`
This is my ouput:
- convertFahrToCelsius(0);: -17.7778
- convertFahrToCelsius("0");:-17.7778
- convertFahrToCelsius([1,2,3]);:1,2,3 is not a valid number but a/an object
- convertFahrToCelsius({temp: 0});:[object Object] is not a valid number but a/an object
I need the last two outputs to be the same as it should be.
Try JSON.stringify()
function convertFahrToCelsius(temp) { if(typeof(temp) === "number" || typeof(temp) === "string"){ return ((Number(temp) - 32) * 5/9).toFixed(4); } else { return `${JSON.stringify(temp)} is not a valid number but a/an ${typeof(temp)}`; } } console.log(convertFahrToCelsius(0)); console.log(convertFahrToCelsius("0") ); console.log(convertFahrToCelsius([1,2,3])); console.log(convertFahrToCelsius({temp: 0}));
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.