javascriptslicetostring

slice is not a function


It gives me an error that slice is not function despite I convert the slice from number to string. where is the issue ?

var weight = prompt("Enter your Weight");
var height = prompt("Enter your Height");
function bmi(weight, height) {
  var result = weight / Math.pow(height, 2);
  var count = result.toString.slice(0, 4);
  if (result < 18.5) {
    alert("Your result is " + count + ", so you are underweight.");
  } else if (result >= 18.5 && result <= 24.9) {
    alert("Your result is " + count + ", so you have a normal weight.");
  } else {
    alert("Your result is " + count + ", so you are overweight.");
  }
}

bmi(weight, height);

I convert the slice from number to string but gives me the same erroe where is the issue ?


Solution

  • You didn't actually call toString().

    toString resolves to a function object, which you would then call with () to get a string. The error you're seeing is telling you that the function object itself does not have a slice() method, which is true.