skip to Main Content

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');

2

Answers


  1. 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');
    Login or Signup to reply.
  2. The issue in your first code snippet lies in the way you’re using the return statement in your conversion function.

    In JavaScript, the return statement can only return a single value.

    When you write return degrees, unit;, it doesn’t actually return both degrees and unit as separate values.

    Instead, it evaluates degrees, unit as an expression and returns only the value of unit.

    To fix this issue, you could modify your conversion function to return an object containing both degrees and unit:

    function conversion(degrees, unit) {
      if (unit === 'F') {
        degrees = (degrees + 32) * 5 / 9;
        unit = 'C';
        return { degrees, unit }; // return an object
      } else if (unit === 'C') {
        degrees = (degrees * 9 / 5) + 32;
        unit = 'F';
        return { degrees, unit }; // return an object
      }
    }
    
    

    And then, in your convertTemp2 function, you can extract the values from the returned object and use them:

    function convertTemp2(degrees, unit) {
      if (unit === 'F' || unit === 'C') {
        const result = conversion(degrees, unit); // extract the values
        console.log(`${result.degrees} ${result.unit}`);
      } else {
        console.log('please use C for celcius, and F for fahrenheit');
      }
    }
    
    

    You can preserve both values and use them correctly in your convertTemp2 function.

    And for your second code snippet it works because it directly logs the converted temperature and unit within the conversion function, without the need for returning any values.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search