samedi 8 mai 2021

How do I check if a variable is a number in Javascript, and if not print out an error message with a if...else loop?

I am new to this and I hope I'm doing this right! Here is my problem. I'm a student in web programming and we are currently studying JavaScript. In the exercise I have to do (one of them!) we need to check if a variable written as a parameter for the unction is a number and if not print a message like 'This is not a number' on the screen.

Here is the function I have. What should I improve for the loop tp work properly? Thank you!

const water = function (temp) {
  if (!Number.isNaN(temp)) {
    let status;
    if (temp < 0) {
      status = "solide";
    }
    if (temp >= 0 && temp < 100) {
      status = "liquide";
    }
    if (temp >= 100) {
      status = "gazeuse";
    }
    console.log(`A ${temp} l'eau est en état ${status}`);
  } else if (isString(temp)) {
    console.log("Ceci n'est pas un nombre");
  }
};

Aucun commentaire:

Enregistrer un commentaire