[ RESOLU ] Boucle while dans une Fonction

Pour toutes les discussions javascript, jQuery et autres frameworks
Répondre
Serenodo
Messages : 11
Enregistré le : 18 févr. 2020, 14:36
Localisation : Savigny le temple
Contact :

[ RESOLU ] Boucle while dans une Fonction

Message par Serenodo » 06 mars 2020, 12:19

Bonjour,
Toujours aussi nul en javascript, j'aimerai ajouter une boucle while dans la fonction afficher. :D :D :D
pour faire un tableau de 7 lignes maximum et du nombre de colonnes tant qu'il y aura des réponses.
voici le code

Code : Tout sélectionner

function afficher()
	{
		while (this.afficher < 7){
		    w.document.writeln( '<table><tr><td><h3><a target="_blank" rel="noopener" href=' + this.url + '>' + this.titre + '</a></h3></td></tr></table>' );
		    while (this.afficher >7 and this.afficher < 14){
		    w.document.writeln( '<table><tr><td><h3><a target="_blank" rel="noopener" href=' + this.url + '>' + this.titre + '</a></h3></td></tr></table>' );
		     while (this.afficher >14and this.afficher < 21){
		    w.document.writeln( '<table><tr><td><h3><a target="_blank" rel="noopener" href=' + this.url + '>' + this.titre + '</a></h3></td></tr></table>' );
		     while (this.afficher >21 and this.afficher < 28){
		    w.document.writeln( '<table><tr><td><h3><a target="_blank" rel="noopener" href=' + this.url + '>' + this.titre + '</a></h3></td></tr></table>' );
		    while (this.afficher >28 ){
		    w.document.writeln( '<h3>TROP DE RÉPONSES ! AJOUTER UN MOT CLE</h3>' );
		}
	}

function Stock( titre_in,keyword_in,url_in )
	{
		this.titre		= titre_in;
		this.keyword	= keyword_in;
		this.url		= url_in;
		this.afficher	= afficher;
	}

	

/* Cette fonction réécrit entièrement la page web pour y afficher les résultats */
	function rechercher( chaine )
	{
		
		var trouved = 0;
		chaine = chaine.toUpperCase();
		var data = new Array();
		store( data );
		w=open('index.html','','');
		/*w.document.writeln( "<html><head><title>Résultats de la recherche</title><link href='css.css' rel='stylesheet' type='text/css'></head>" );*/
		/*w.document.writeln( "<body><div id='Corps'><h3>Résultats de la recherche "+chaine+"</h3></div>" );*/
		w.document.writeln("<center><p style='color: blue; font-weight: bold;'><h3> Résultats de la recherche "+chaine+"</h3></p></center>");
		
		for( var i=0; i<data.length ; i++ )
		{
			if(( data[i].titre.toUpperCase().indexOf( chaine ) != -1 )
			|| ( data[i].keyword.toUpperCase().indexOf( chaine ) != -1 )
			|| ( data[i].url.toUpperCase().indexOf( chaine ) != -1 ))
			{
				data[i].afficher();
				trouved ++;
			}
		}
		if( !trouved )
		{
			w.document.writeln( "<p style='color: red; font-weight: bold;'>Aucune réponse n'a été trouvée pour ce mot clé dans Base_Doc.</p>");
		}

		/*w.document.writeln( "<p style='color: blue; font-weight: bold;'>"+trouved+" réponse(s) trouvée(s) pour le mot-clé "+chaine);*/
		w.document.writeln( "</p><hr><form><input type='button' value='Nouvelle recherche ?' OnClick='window.location=\"index.html\"'></form></div>" );
		w.document.writeln( "</body></html>" );
		w.document.close();
	}

//-->
	</script>

Merci
Modifié en dernier par Serenodo le 11 mars 2020, 07:53, modifié 1 fois.
" L'écriture est mon rempart conte la folie " ----- " Ne laissez personne détruire vos rêves "

Avatar du membre
webmaster
Administrateur du site
Messages : 574
Enregistré le : 28 févr. 2017, 15:19

Re: Boucle while dans une Fonction

Message par webmaster » 07 mars 2020, 10:54

Bonjour,

Il manque la partie HTML
Le script est un peu compliqué sans une page complete pour tester correctement

PS : On remarque déjà une erreur dans la console :
and n'existe pas en JS
il faut écrire &&
TJS : 25 ans et mon livre Tout JavaScript chez Dunod
https://www.toutjavascript.com/livre/index.php

Serenodo
Messages : 11
Enregistré le : 18 févr. 2020, 14:36
Localisation : Savigny le temple
Contact :

Re: Boucle while dans une Fonction

Message par Serenodo » 09 mars 2020, 10:52

Bonjour,

Voici le fichier html.

Code : Tout sélectionner

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<link href="css.css" rel="stylesheet" type="text/css" />
<title>Base de Connaissance</title>
    <script src="19 - BaseDoc\data.js"></script>
    <script type = 'text/javascript'>
<!--
/* FUNCTION INCLUDE */



function afficher()
	{
		while (this.afficher < 7){
		    w.document.writeln( '<table><tr><td><h3><a target="_blank" rel="noopener" href=' + this.url + '>' + this.titre + '</a></h3></td></tr></table>' );
		    while (this.afficher >7 && this.afficher < 14){
		    w.document.writeln( '<table><tr><td><h3><a target="_blank" rel="noopener" href=' + this.url + '>' + this.titre + '</a></h3></td></tr></table>' );
		     while (this.afficher >14&& this.afficher < 21){
		    w.document.writeln( '<table><tr><td><h3><a target="_blank" rel="noopener" href=' + this.url + '>' + this.titre + '</a></h3></td></tr></table>' );
		     while (this.afficher >21 && this.afficher < 28){
		    w.document.writeln( '<table><tr><td><h3><a target="_blank" rel="noopener" href=' + this.url + '>' + this.titre + '</a></h3></td></tr></table>' );
		    while (this.afficher >28 ){
		    w.document.writeln( '<h3>TROP DE RÉPONSES ! AJOUTER UN MOT CLE</h3>' );
		}
	}

function Stock( titre_in,keyword_in,url_in )
	{
		this.titre		= titre_in;
		this.keyword	= keyword_in;
		this.url		= url_in;
		this.afficher	= afficher;
	}

	

/* Cette fonction réécrit entièrement la page web pour y afficher les résultats */
	function rechercher( chaine )
	{
		
		var trouved = 0;
		chaine = chaine.toUpperCase();
		var data = new Array();
		store( data );
		w=open('index.html','','');
		/*w.document.writeln( "<html><head><title>Résultats de la recherche</title><link href='css.css' rel='stylesheet' type='text/css'></head>" );*/
		/*w.document.writeln( "<body><div id='Corps'><h3>Résultats de la recherche "+chaine+"</h3></div>" );*/
		w.document.writeln("<center><p style='color: blue; font-weight: bold;'><h3> Résultats de la recherche "+chaine+"</h3></p></center>");
		
		for( var i=0; i<data.length ; i++ )
		{
			if(( data[i].titre.toUpperCase().indexOf( chaine ) != -1 )
			|| ( data[i].keyword.toUpperCase().indexOf( chaine ) != -1 )
			|| ( data[i].url.toUpperCase().indexOf( chaine ) != -1 ))
			{
				data[i].afficher();
				trouved ++;
			}
		}
		if( !trouved )
		{
			w.document.writeln( "<p style='color: red; font-weight: bold;'>Aucune réponse n'a été trouvée pour ce mot clé dans Base_Doc.</p>");
		}

		/*w.document.writeln( "<p style='color: blue; font-weight: bold;'>"+trouved+" réponse(s) trouvée(s) pour le mot-clé "+chaine);*/
		w.document.writeln( "</p><hr><form><input type='button' value='Nouvelle recherche ?' OnClick='window.location=\"index.html\"'></form></div>" );
		w.document.writeln( "</body></html>" );
		w.document.close();
	}

//-->
	</script>


</head>

<body>
	
	<div id="Header0">
	<center>
	<img src="img/1a.png"/>
	</center>
	</div>
		<!--- HEADER --->
	<div id="Header1">
		
		<div class="HeaderRecherche">
			
			<center>
				<form name="rech" OnSubmit="rechercher( document.rech.champ.value );">
				<input type="text" name="champ"    size="70"><BR>
				<input type="submit" name="valide" value="Rechercher">
				</form>


				<noscript><p>
				Votre navigateur ne reconnaît pas le JavaScript, le script de recherche ne pourra pas marcher.
				</p></noscript>
			</center>
			
		</div>
		
	</div>
	<!--- END HEADER --->
	<!--- CENTRAL --->
	<div id="Corps">
				
	</div>
	<!--- END CENTRAL --->
	<!--- FOOTER --->
	<div id="Footer">
	<!--- Tableau  6 Colonnes -- 10 Lignes dont une en titre-->
	<table>
		<!--- TITRES--->
		<thead>	
		<tr>
		<th><span style='color:black; font-weight:bold' >WEB</span> </th>
		<th><span style='color:black; font-weight:bold' >MEMO</span> </th>
		<th><span style='color:black; font-weight:bold' >ANNUAIRES</span> </th>
		<th><span style='color:black; font-weight:bold' >DICTIONNAIRE</span> </th>
		<th><span style='color:black; font-weight:bold' >MODELES</span> </th>
		<th><span style='color:black; font-weight:bold' >SITES</span> </th>
		</tr>
		</thead>	
		<!--- Ligne1 --->
		<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		</tr>
		<!--- Ligne3--->
		<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		</tr>
		<!--- Ligne4--->
		<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		</tr>
		<!--- Ligne5--->
		<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>	
		</tr>
		<!--- Ligne6 --->
		<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		</tr>
		<!--- Ligne7 --->
		<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		</tr>
		<!--- Ligne8 --->
		<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		</tr>
		<!--- Ligne9 --->
		<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		</tr>
		<!--- Ligne10 --->
		<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		</tr>
		</table>
	</div>
	<!--- END FOOTER --->
</body>

</html>

Merci pour ton aide.
" L'écriture est mon rempart conte la folie " ----- " Ne laissez personne détruire vos rêves "

Avatar du membre
webmaster
Administrateur du site
Messages : 574
Enregistré le : 28 févr. 2017, 15:19

Re: Boucle while dans une Fonction

Message par webmaster » 10 mars 2020, 12:56

Il manque aussi data.js

Merci
TJS : 25 ans et mon livre Tout JavaScript chez Dunod
https://www.toutjavascript.com/livre/index.php

Serenodo
Messages : 11
Enregistré le : 18 févr. 2020, 14:36
Localisation : Savigny le temple
Contact :

Re: Boucle while dans une Fonction

Message par Serenodo » 10 mars 2020, 13:56

Bonjour,
Désolé.
Voici

Code : Tout sélectionner

function store (data)
{
		data[0]=new Stock("BIBLI POUR CONTES","","02-2020","doc/Bpc.pdf" );
		data[1]=new Stock("CONNEXION ENTRE DEUX SCENES","","02-2020","doc/connexion.pdf" );
		data[2]=new Stock("PERSONNAGE ","","03-2020","doc/personnage.pdf" );
		data[3]=new Stock("MATRICE NOUVELLE","","03-2020","doc/NouvelleMatrice.pdf" );
		data[4]=new Stock("REVISION RELECTURE RAPIDE","","03-2020","doc/RelectureRapide.pdf" );
		/*data[5]=new Stock("","","","doc/");*/
		/*data[6]=new Stock("","","","doc/");*/
		/*data[7]=new Stock("","","","doc/");*/
		/*data[8]=new Stock("","","","doc/");*/
		/*data[9]=new Stock("","","","doc/");*/
		/*data[10]=new Stock("","","","doc/");*/
		/*data[11]=new Stock("","","","doc/");*/
		/*data[12]=new Stock("","","","doc/");*/
		/*data[13]=new Stock("","","","doc/");*/
		/*data[14]=new Stock("","","","doc/");*/
		/*data[15]=new Stock("","","","doc/");*/
		/*data[16]=new Stock("","","","doc/");*/
		/*data[17]=new Stock("","","","doc/");*/
		/*data[18]=new Stock("","","","doc/");*/
		/*data[19]=new Stock("","","","doc/");*/
		/*data[20]=new Stock("","","","doc/");*/
		/*data[21]=new Stock("","","","doc/");*/
		/*data[22]=new Stock("","","","doc/");*/
		/*data[23]=new Stock("","","","doc/");*/
		/*data[24]=new Stock("","","","doc/");*/
		/*data[25]=new Stock("","","","doc/");*/
		/*data[26]=new Stock("","","","doc/");*/
		/*data[27]=new Stock("","","","doc/");*/
		/*data[28]=new Stock("","","","doc/");*/
		/*data[29]=new Stock("","","","doc/");*/
		/*data[30]=new Stock("","","","doc/");*/
		/*data[31]=new Stock("","","","doc/");*/
		/*data[32]=new Stock("","","","doc/");*/
		/*data[33]=new Stock("","","","doc/");*/
		/*data[34]=new Stock("","","","doc/");*/
		/*data[35]=new Stock("","","","doc/");*/
		/*data[36]=new Stock("","","","doc/");*/
		/*data[37]=new Stock("","","","doc/");*/
		/*data[38]=new Stock("","","","doc/");*/
		/*data[39]=new Stock("","","","doc/");*/
		/*data[40]=new Stock("","","","doc/");*/
		/*data[41]=new Stock("","","","doc/");*/
		/*data[42]=new Stock("","","","doc/");*/
		/*data[43]=new Stock("","","","doc/");*/
		/*data[44]=new Stock("","","","doc/");*/	
}
" L'écriture est mon rempart conte la folie " ----- " Ne laissez personne détruire vos rêves "

Avatar du membre
webmaster
Administrateur du site
Messages : 574
Enregistré le : 28 févr. 2017, 15:19

Re: Boucle while dans une Fonction

Message par webmaster » 10 mars 2020, 16:12

Ok, j'ai compris

Il n'y a pas besoin de boucle while : il y a deja une boucle qui recherche les résultats
C'est a ce moment qu'il faut afficher les résultats selon le nombres trouvés (dans trouved)

J'ai fait un démarrage ici
https://www.toutjavascript.com/user/boucle-while.html
TJS : 25 ans et mon livre Tout JavaScript chez Dunod
https://www.toutjavascript.com/livre/index.php

Serenodo
Messages : 11
Enregistré le : 18 févr. 2020, 14:36
Localisation : Savigny le temple
Contact :

Re: Boucle while dans une Fonction

Message par Serenodo » 11 mars 2020, 07:53

Bonjour Webmaster,

Super génial.
Je vais pouvoir poser mon autre question.
Merci beaucoup.
Bonne journée.
" L'écriture est mon rempart conte la folie " ----- " Ne laissez personne détruire vos rêves "

Répondre