Function Return Values, adding fourth function in active learning exercise

Although I got the active learning code to work, when I attempt to add a fourth or fifth function to the code, using the same format as the prior code, it does not work at all. I can’t figure out why. Anyone have an idea?

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Function library example</title>
  <style>
    input {
      font-size: 2em;
      margin: 10px 1px 0;
    }
  </style>
</head>
<body>

  <input class="numberInput" type="text">
  <p></p>
  
  <script>
    var input = document.querySelector('.numberInput');
    var para = document.querySelector('p');

    function squared(num) {
      return num * num;
    }

    function quartic(num) {
      return num * num * num *num;
    }

    function factorial(num) {
      var x = num;
      while (x > 1) {
        num *= x-1;
        x--;
    }

    function cube(num) {
      return num * num * num;
    }

    return num;
    }

    input.onchange = function() {
      var num = input.value;
      if(isNaN(num)) {
        para.textContent = 'You need to enter a number!';
      } else {
        para.textContent = num + ' squared is ' + squared(num) + '. '+
                           num + ' quartic is ' + quartic(num) + '. ' +
                           num + ' factorial is ' + factorial(num) + '. ' +
                           num + ' cube is ' + cube(num) + '. ';
                           
      }
    }
  </script>
</body>
</html>

You have to move the function “cube” outside the function “factorial”.

1 Like

You need to correct the above to:

function factorial(num) {
  var x = num;
  while (x > 1) {
    num *= x-1; x--;
  }

  return num;
}

function cube(num) {
  return num * num * num;
}
1 Like

ExE-Boss’s solution fixed it for me. Thank you!!

1 Like