variable doesn't retain value in JavaScript -


alert("bienvenue dans le gestionnaire des contacts !");  alert("1 : lister les contacts\n2 : ajouter un contact\n0 : quitter\n");    var contacts = {    nom: function(nom) {      this.nom = nom;    },    prenom: function(prenom) {      this.prenom = prenom;    }  };    var personne1 = object.create(contacts);  personne1.nom("lévisse");  personne1.prenom("carole");    var personne2 = object.create(contacts);  personne2.nom("nelsonne");  personne2.prenom("mélodie");    var contacts = [];  contacts.push(personne1);  contacts.push(personne2);    var i;  var boucle = 1;  var choix = 1;  while (boucle == 1) {    var choix = prompt("entrez une valeur :");    if (choix === 1) {      alert("voici la liste de tout vos contacts : ");      (i = 0; < contacts.length; += 1) {        alert("nom : " + contacts[i].nom + ", prenom : " + contacts[i].prenom + "\n");      }    } else if (choix === 2) {      boucle = 0;    } else if (choix === 0) {      alert("au revoir.");      boucle = 0;    }      alert("1 : lister les contacts\n2 : ajouter un contact\n0 : quitter\n");  }

whenever enter 1/2/0 in choix variable never enters in if condition reason, why? tho see here made sure prompt put entered choice in same exact variable...

that because prompt return string value , you're trying compare integer value, can use == instead of === since triple equals check type of variables, or can add quotes in condition :

if (choix == 1) { //or if (choix === '1') { 

another approach casting returned string int using parseint() function :

var choix = parseint( prompt("entrez une valeur :") ); 

or number() :

var choix = number(prompt("entrez une valeur :")); 

hope helps.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -