I am trying to create a function that converts celcius to fahrenheit and vice versa using a return statement inside. I'm confused why the code below won't return or log anything on the console but the bottom bit of code will.
function convertTemp2(degrees,unit) {
if (unit === 'F' || unit === 'C') {
conversion(degrees,unit);
console.log(`${degrees} ${unit}`)
} else {
console.log('please use C for celcius, and F for fahrenheit');
}
}
function conversion(degrees,unit) {
if (unit === 'F') {
degrees = (degrees+32) *5/9;
unit = 'C';
return degrees, unit;
} else if (unit === 'C') {
degrees = (degrees*9/5) + 32;
unit = 'F';
return degrees, unit;
}
}
convertTemp2(1023,'F');
convertTemp2(12,'C');
convertTemp2(123,'H');
This seems to work:
function convertTemp2(degrees,unit) {
if (unit === 'F' || unit === 'C') {
conversion(degrees,unit);
} else {
console.log('please use C for celcius, and F for fahrenheit');
}
}
function conversion(degrees,unit) {
if (unit === 'F') {
degrees = (degrees+32) *5/9;
console.log(`${degrees}C`)
} else if (unit === 'C') {
degrees = (degrees*9/5) + 32;
console.log(`${degrees}F`)
}
}
convertTemp2(1023,'F');
convertTemp2(12,'C');
convertTemp2(123,'H');
it should look like
function convertTemp2(degrees,unit) {
if (unit === 'F' || unit === 'C') {
const {a, b} = conversion(degrees,unit);
console.log(`${a} ${b}`)
} else {
console.log('please use C for celcius, and F for fahrenheit');
}
}
function conversion(degrees,unit) {
let a, b
if (unit === 'F') {
a = (degrees+32) *5/9;
b = 'C';
return {a,b};
} else if (unit === 'C') {
a = (degrees*9/5) + 32;
b = 'F';
return {a, b};
}
}
convertTemp2(1023,'F');
// convertTemp2(12,'C');
// convertTemp2(123,'H');