Comment incrémenter une boucle avec des conditions

Résolu/Fermé
flexi2202 Messages postés 3795 Date d'inscription lundi 14 mars 2011 Statut Membre Dernière intervention 21 avril 2024 - 22 août 2022 à 08:03
flexi2202 Messages postés 3795 Date d'inscription lundi 14 mars 2011 Statut Membre Dernière intervention 21 avril 2024 - 22 août 2022 à 15:51

Bonjour a tous

je souhaiterais incrémenter ma boucle afin de découvrir combien d'article il y a dans le panier en tenant en compte le  type de nom

Par exemple si le nom de l'article est 77777 on incrémente de 10

Si le nom de l'article est 88888 on incrémente de 20

si non on incrémente de 1

cela fonctionne pour 1 passage 

Mais si j'ajoute par exemple 4 fois l'article 77777 cela me retourne 10 au lieu de 40

voici ma boucle

	  var count=0;
	
	for(var i in panierArray) {
		output += "<div class='row' style='border-style: ridge;  border-width: 1px; border-color: #8ebf42; background-color: #d9d9d9;margin-bottom:5px;'>"
		+ "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'><button class='btn btn-danger effacer-item' data-nom='" + panierArray[i].nom + "'>X</button></div>"
		 + "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'>" + " <a class='example-image-link' href='"+ panierArray[i].url +"'data-lightbox='example-set'><img src=""  style='width:100px;height:100px;'></a>" + "</div>"
		  + "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'>" + panierArray[i].nom + "</div>" 
		  + "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'>" + panierArray[i].prix.toFixed(0) + "  euro</div>"
		 + "<div class='form-inline col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'><div class='input-group'><button class='btn btn-primary moins-item' data-nom='" + panierArray[i].nom + "'>-</button>"
		 + "<input type='number' min='1' class='form-control item-quantite' style='width:55px !important' data-nom='" + panierArray[i].nom + "' value='" + panierArray[i].quantite + "'>"
		 + "<button class='btn btn-primary plus-item' data-nom='" + panierArray[i].nom + "'>+</button></div></div>"
	     + "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'>" + panierArray[i].total + " euros</div>" 
         +  "</div>";
      if (panierArray[i].nom == 77777) {
 //on incremente count de 10
 count=count +10
} else if (panierArray[i].nom == 88888) {
  //on incremente count de 20
  count=count +20
} else if (panierArray[i].nom == 99999) {
  //on incremente count de 20
  count=count +30
}else {
  count=count +1
}
var counttotal =count;
console.log(counttotal);
	}


Windows / Chrome 103.0.5060.66

7 réponses

yg_be Messages postés 22731 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 27 avril 2024 1 477
22 août 2022 à 09:12

bonjour,

moi, si je voulais savoir ce que faisait la boucle, j'ajouterais, dans la boucle un console.log() avec les valeur de i, panierArray[i].nom, panierArray[i].quantite et count.

1
flexi2202 Messages postés 3795 Date d'inscription lundi 14 mars 2011 Statut Membre Dernière intervention 21 avril 2024 187
Modifié le 22 août 2022 à 10:14

bonjour yg_be

oui bonne idée

voici ce que cela donne 

console.log(i); 1

console.log(panierArray[i].nom);77777

console.log(panierArray[i].quantite);1

console.log(count);10

console.log(counttotal);10

mais si j'ajoute par exemple un article numéro 2006 cela devient 

si j'aoute encore 1 fois le numero 2006

0
yg_be Messages postés 22731 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 27 avril 2024 1 477 > flexi2202 Messages postés 3795 Date d'inscription lundi 14 mars 2011 Statut Membre Dernière intervention 21 avril 2024
22 août 2022 à 10:18

Il est préférable de partager du texte, pas des images.

1
flexi2202 Messages postés 3795 Date d'inscription lundi 14 mars 2011 Statut Membre Dernière intervention 21 avril 2024 187 > yg_be Messages postés 22731 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 27 avril 2024
22 août 2022 à 10:49

je reviens plus tard ...

j'ai un bug dans le code

0
yg_be Messages postés 22731 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 27 avril 2024 1 477
22 août 2022 à 09:14

Quand tu écris "j'ajoute par exemple 4 fois l'article 77777", cela signifie bien que tu as 4 lignes dans le panier pour cet article?

1
yg_be Messages postés 22731 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 27 avril 2024 1 477
22 août 2022 à 14:02

plus propre:

var f = 0
	for(var i in panierArray) {
		output += "<div class='row' style='border-style: ridge;  border-width: 1px; border-color: #8ebf42; background-color: #d9d9d9;margin-bottom:5px;'>"
		+ "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'><button class='btn btn-danger effacer-item' data-nom='" + panierArray[i].nom + "'>X</button></div>"
		 + "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'>" + " <a class='example-image-link' href='"+ panierArray[i].url +"'data-lightbox='example-set'><img src=""""  style='width:100px;height:100px;'></a>" + "</div>"
		  + "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'>" + panierArray[i].nom + "</div>" 
		  + "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'>" + panierArray[i].prix.toFixed(0) + "  euro</div>"
		 + "<div class='form-inline col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'><div class='input-group'><button class='btn btn-primary moins-item' data-nom='" + panierArray[i].nom + "'>-</button>"
		 + "<input type='number' min='1' class='form-control item-quantite' style='width:55px !important' data-nom='" + panierArray[i].nom + "' value='" + panierArray[i].quantite + "'>"
		 + "<button class='btn btn-primary plus-item' data-nom='" + panierArray[i].nom + "'>+</button></div></div>"
	     + "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'>" + panierArray[i].total + " euros</div>" 
         +  "</div>";
if (panierArray[i].nom == 77777) {
 f=10
} else if (panierArray[i].nom == 88888) {
  f=20
} else if (panierArray[i].nom == 99999) {
  f=30
}else {
f=1
}
count=count + (f * Number(panierArray[i].quantite))
 console.log(count);
1
flexi2202 Messages postés 3795 Date d'inscription lundi 14 mars 2011 Statut Membre Dernière intervention 21 avril 2024 187
22 août 2022 à 09:29

bonjour yg_be

oui voila 4 lignes avec l'articles 77777

Enfin pas vraiment 4 lignes , mais plutôt une seule ligne avec la quantité a 4

comme cet exemple

0
yg_be Messages postés 22731 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 27 avril 2024 1 477
22 août 2022 à 09:51

Ton code compte les lignes, et il y a une seule ligne.
Où dans ta boucle tiens-tu compte de la quantité?

Le code ne peut pas deviner ce que tu souhaites, il fait ce que tu lui ordonnes de faire.

Montre ce que donne ma suggestion en #1.

1

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
flexi2202 Messages postés 3795 Date d'inscription lundi 14 mars 2011 Statut Membre Dernière intervention 21 avril 2024 187
22 août 2022 à 10:22

non je veux compter les quantités pas les lignes 

je me sers de count pour essayer de compter la quantité les articles 

Mais dans la boucle cela se passe avec la variable panierArray[i].quantite

0
yg_be Messages postés 22731 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 27 avril 2024 1 477
22 août 2022 à 10:45

Cela marche bien, maintenant que tu utilises panierArray[i].quantite? 

0
flexi2202 Messages postés 3795 Date d'inscription lundi 14 mars 2011 Statut Membre Dernière intervention 21 avril 2024 187 > yg_be Messages postés 22731 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 27 avril 2024
Modifié le 22 août 2022 à 11:11

voici ce que donne les consoles.log

si j'ajoute 77777

nombre de i******0 
panierArray[i].nom ******77777 
panierArray[i].quantite**** 1 
count *****10 
counttotal *****10 

si j'ajoute un article

nombre de i******0 
panierArray[i].nom ******77777 
panierArray[i].quantite**** 1 p
count *****10 
counttotal *****10 
nombre de i******1 
panierArray[i].nom ******2006 
panierArray[i].quantite**** 1 
count *****11 
counttotal *****11 

a présent je change la quantité de 2006 et je la mets a deux 

nombre de i******0 
panierArray[i].nom ******77777 
panierArray[i].quantite**** 1 
count *****10 
counttotal *****10 
nombre de i******1 
panierArray[i].nom ******2006 
panierArray[i].quantite**** 2 
count *****11 
counttotal *****11 

cela aurait du évoluer a 12pour count et counttotal

0
jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 4 650 > flexi2202 Messages postés 3795 Date d'inscription lundi 14 mars 2011 Statut Membre Dernière intervention 21 avril 2024
22 août 2022 à 11:31

Il te suffit donc de multiplier la quantité par les coef que tu veux...

1
flexi2202 Messages postés 3795 Date d'inscription lundi 14 mars 2011 Statut Membre Dernière intervention 21 avril 2024 187 > jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024
22 août 2022 à 12:05

Merci jordane 

je pense avoir compris 

je peux faire ceci 

      if (panierArray[i].nom == 77777) {
 //on incremente count de 10
var cont = Number(panierArray[i].quantite) *10
console.log(cont);
}
0
jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 4 650 > flexi2202 Messages postés 3795 Date d'inscription lundi 14 mars 2011 Statut Membre Dernière intervention 21 avril 2024
22 août 2022 à 12:07

plutôt

cont += Number(panierArray[i].quantite) *10

si tu veux que ton compteur s'incrémente pour chaque produit...

1
flexi2202 Messages postés 3795 Date d'inscription lundi 14 mars 2011 Statut Membre Dernière intervention 21 avril 2024 187
22 août 2022 à 12:24

Merci jordane mais en fessant de cette manière j'obtiens ce message d'erreur

Uncaught SyntaxError: unexpected token: '+='

voici mon code

	for(var i in panierArray) {
		output += "<div class='row' style='border-style: ridge;  border-width: 1px; border-color: #8ebf42; background-color: #d9d9d9;margin-bottom:5px;'>"
		+ "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'><button class='btn btn-danger effacer-item' data-nom='" + panierArray[i].nom + "'>X</button></div>"
		 + "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'>" + " <a class='example-image-link' href='"+ panierArray[i].url +"'data-lightbox='example-set'><img src=""  style='width:100px;height:100px;'></a>" + "</div>"
		  + "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'>" + panierArray[i].nom + "</div>" 
		  + "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'>" + panierArray[i].prix.toFixed(0) + "  euro</div>"
		 + "<div class='form-inline col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'><div class='input-group'><button class='btn btn-primary moins-item' data-nom='" + panierArray[i].nom + "'>-</button>"
		 + "<input type='number' min='1' class='form-control item-quantite' style='width:55px !important' data-nom='" + panierArray[i].nom + "' value='" + panierArray[i].quantite + "'>"
		 + "<button class='btn btn-primary plus-item' data-nom='" + panierArray[i].nom + "'>+</button></div></div>"
	     + "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'>" + panierArray[i].total + " euros</div>" 
         +  "</div>";
        if (panierArray[i].nom == 77777) {
 //on incremente count de 10
var cont1 += Number(panierArray[i].quantite) *10
console.log(cont1);
} else if (panierArray[i].nom == 88888) {
  //on incremente count de 20
var cont2 += Number(panierArray[i].quantite) *20
console.log(cont2);
} else if (panierArray[i].nom == 99999) {
  //on incremente count de 20
var cont3 += Number(panierArray[i].quantite) *30
console.log(cont3);
}else {
var cont4 += Number(panierArray[i].quantite) *1;
 console.log(cont4);
}

var total= Number (cont1) + Number (cont2) + Number (cont3) + Number (cont4) ;
console.log(total);
//console.log("nombre de i******" + i + " "); 
//console.log("panierArray[i].nom ******" + panierArray[i].nom  + " ");
//console.log("panierArray[i].quantite**** " + panierArray[i].quantite + " ");
//console.log("count *****" + count + " ");
//counttotal=count;
//console.log("counttotal *****" + counttotal + " ");
	}
0
yg_be Messages postés 22731 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 27 avril 2024 1 477
22 août 2022 à 12:59

avant tu faisais 

count=count +10

si tu veux tenir compte de la quantité:

count=count + (10 * Number(panierArray[i].quantite))
1
flexi2202 Messages postés 3795 Date d'inscription lundi 14 mars 2011 Statut Membre Dernière intervention 21 avril 2024 187 > yg_be Messages postés 22731 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 27 avril 2024
22 août 2022 à 13:46

super pour ce qui est des quantité cela semble fonctionner 

par contre je ne sais pas si je m'y prends bien pour tout additionner 

car la somme de mes 4 conditions n'est pas bonne 

il prends en charge la dernière quantité que je rentre pour chaque condition 

ce qui semble normal 

Mais comment additionner mes conditions  

	var panierArray = MonPanier.listpanier();
	var output = "";
	 var countart=0;
    var countart2=0;
    var count=0;
    var count1=0;
    var count2=0;
    var count3=0;
    var count4=0;
	for(var i in panierArray) {
		output += "<div class='row' style='border-style: ridge;  border-width: 1px; border-color: #8ebf42; background-color: #d9d9d9;margin-bottom:5px;'>"
		+ "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'><button class='btn btn-danger effacer-item' data-nom='" + panierArray[i].nom + "'>X</button></div>"
		 + "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'>" + " <a class='example-image-link' href='"+ panierArray[i].url +"'data-lightbox='example-set'><img src=""  style='width:100px;height:100px;'></a>" + "</div>"
		  + "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'>" + panierArray[i].nom + "</div>" 
		  + "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'>" + panierArray[i].prix.toFixed(0) + "  euro</div>"
		 + "<div class='form-inline col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'><div class='input-group'><button class='btn btn-primary moins-item' data-nom='" + panierArray[i].nom + "'>-</button>"
		 + "<input type='number' min='1' class='form-control item-quantite' style='width:55px !important' data-nom='" + panierArray[i].nom + "' value='" + panierArray[i].quantite + "'>"
		 + "<button class='btn btn-primary plus-item' data-nom='" + panierArray[i].nom + "'>+</button></div></div>"
	     + "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'>" + panierArray[i].total + " euros</div>" 
         +  "</div>";
        if (panierArray[i].nom == 77777) {
 //on incremente count de 10
count1=count + (10 * Number(panierArray[i].quantite))
console.log(count1);
} else if (panierArray[i].nom == 88888) {
  //on incremente count de 20
count2=count + (20 * Number(panierArray[i].quantite))
console.log(count2);
} else if (panierArray[i].nom == 99999) {
  //on incremente count de 20
count3=count + (30 * Number(panierArray[i].quantite))
console.log(ccount3);
}else {
count4=count + (1 * Number(panierArray[i].quantite))
 console.log(count4);
}
console.log("total");
var total= Number (count1) + Number (count2) + Number (count3) + Number (count4) ;
console.log(total);
//console.log("nombre de i******" + i + " "); 
//console.log("panierArray[i].nom ******" + panierArray[i].nom  + " ");
//console.log("panierArray[i].quantite**** " + panierArray[i].quantite + " ");
//console.log("count *****" + count + " ");
//counttotal=count;
//console.log("counttotal *****" + counttotal + " ");
	}
0
flexi2202 Messages postés 3795 Date d'inscription lundi 14 mars 2011 Statut Membre Dernière intervention 21 avril 2024 187 > flexi2202 Messages postés 3795 Date d'inscription lundi 14 mars 2011 Statut Membre Dernière intervention 21 avril 2024
Modifié le 22 août 2022 à 13:49

je pense avoir compris 

voila le bon code 

	var panierArray = MonPanier.listpanier();
	var output = "";
	 var countart=0;
    var countart2=0;
    var count=0;
    //var count1=0;
    //var count2=0;
    //var count3=0;
    //var count4=0;
	for(var i in panierArray) {
		output += "<div class='row' style='border-style: ridge;  border-width: 1px; border-color: #8ebf42; background-color: #d9d9d9;margin-bottom:5px;'>"
		+ "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'><button class='btn btn-danger effacer-item' data-nom='" + panierArray[i].nom + "'>X</button></div>"
		 + "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'>" + " <a class='example-image-link' href='"+ panierArray[i].url +"'data-lightbox='example-set'><img src="""  style='width:100px;height:100px;'></a>" + "</div>"
		  + "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'>" + panierArray[i].nom + "</div>" 
		  + "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'>" + panierArray[i].prix.toFixed(0) + "  euro</div>"
		 + "<div class='form-inline col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'><div class='input-group'><button class='btn btn-primary moins-item' data-nom='" + panierArray[i].nom + "'>-</button>"
		 + "<input type='number' min='1' class='form-control item-quantite' style='width:55px !important' data-nom='" + panierArray[i].nom + "' value='" + panierArray[i].quantite + "'>"
		 + "<button class='btn btn-primary plus-item' data-nom='" + panierArray[i].nom + "'>+</button></div></div>"
	     + "<div class='col' style='text-align: center;border-left: solid;padding-left: 5px;padding-right: 5px;'>" + panierArray[i].total + " euros</div>" 
         +  "</div>";
        if (panierArray[i].nom == 77777) {
 //on incremente count de 10
count=count + (10 * Number(panierArray[i].quantite))
console.log(count);
} else if (panierArray[i].nom == 88888) {
  //on incremente count de 20
count=count + (20 * Number(panierArray[i].quantite))
console.log(count);
} else if (panierArray[i].nom == 99999) {
  //on incremente count de 20
count=count + (30 * Number(panierArray[i].quantite))
console.log(ccount);
}else {
count=count + (1 * Number(panierArray[i].quantite))
 console.log(count);
}
console.log(count);
//var total= Number (count1) + Number (count2) + Number (count3) + Number (count4) ;
console.log(total);
//console.log("nombre de i******" + i + " "); 
//console.log("panierArray[i].nom ******" + panierArray[i].nom  + " ");
//console.log("panierArray[i].quantite**** " + panierArray[i].quantite + " ");
//console.log("count *****" + count + " ");
//counttotal=count;
//console.log("counttotal *****" + counttotal + " ");
	}
0
flexi2202 Messages postés 3795 Date d'inscription lundi 14 mars 2011 Statut Membre Dernière intervention 21 avril 2024 187
22 août 2022 à 15:51

Merci pour la solution 

Je vais tester ...

Mais a première vue cela fonctionne

0