Changer la couleur des consonnes en rouge et voyelles en bleu

Résolu
Madonnina78 Messages postés 27 Date d'inscription vendredi 26 novembre 2010 Statut Membre Dernière intervention 9 juin 2012 - 22 mai 2012 à 09:54
cs_L0ci Messages postés 224 Date d'inscription vendredi 26 novembre 2010 Statut Membre Dernière intervention 11 juin 2013 - 22 mai 2012 à 16:02
Bonjour,

l'exercice c'est:

l'utilisateur entre une phrase dans un champ texte, il clique sur changer et sa phrase apparaît dans la div en dessous mais les consonne d'une couleur et les voyelles d'une autre.

J'ai essayé de tout mais je n'y parviens pas si quelqu'un peut m'aider svp?

D'avance merci.


<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Essaie des ID</title>


</head>



<script type="text/javascript">

function travail()

{	// function

var userInput = document.getElementById('userInput');
var o = document.getElementById("mondiv");
var name = userInput.value 
var consonne= 'cdfghjklmnpqrstvwxyz';
var voyelle= 'aeiou';


for (count=0; count<name.length; count++)  

{ // for name count

daChar = name.charCodeAt(count);

for (i = 0; i < consonne.length; i++) 

{ // for consonne i

if (daChar == consonne.charCodeAt(i))

{ // if consonne

o.innerHTML = ''+ name +''; 							

} // end if consonne	

}	// end for consonne

for (a = 0; a < voyelle.length; a++) 

{ // for voyelle a

if (daChar == voyelle.charCodeAt(a))

{ // if voyelle

o.innerHTML = ''+ name + ''; 

} // end if voyelle

}	// end for voyelle


} // end for count	
} // end function

</script>





 test


</html>

10 réponses

Madonnina78 Messages postés 27 Date d'inscription vendredi 26 novembre 2010 Statut Membre Dernière intervention 9 juin 2012
22 mai 2012 à 15:58
Merci pour ta précieuse aide. j'y suis arrivée :p

je met le code ça pourrait peut-être aider quelqu'un un jour.

Bonne fin de journée,

Rosa

<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Essaie des ID </title>


</head>



<script type="text/javascript">

function travail()

{	// function

var userInput = document.getElementById('userInput');
var o = document.getElementById("mondiv");
var name = userInput.value 
var consonneMin = 'cdfghjklmnpqrstvwxyz';
var consonneMaj = consonneMin.toUpperCase();
var consonne = consonneMin + consonneMaj
var voyelleMin = 'aeiou';
var voyelleMaj = voyelleMin.toUpperCase();
var voyelle = voyelleMin + voyelleMaj + ':?';
var espace = ' ';


for (count=0; count<name.length; count++)



{ // for name count

var daChar = name.charAt(count);
var result = "";

for (i = 0; i < consonne.length; i++) 

{ // for consonne i

if (daChar == consonne.charAt(i))

{ // if consonne



result += ''+ daChar + '';


} // end if consonne	

}	// end for consonne

for (a = 0; a < voyelle.length; a++) 

{ // for voyelle a

if (daChar == voyelle.charAt(a))

{ // if voyelle


result += ''+ daChar + '';						 

} // end if voyelle				

}	// end for voyelle

for (b = 0; b < espace.length; b++) 

{ // for espace

if (daChar == espace.charAt(b))

{ // if espace


result += ''+ daChar + '';						 

} // end if espace				

}	// end for espace

o.innerHTML += result;


} // end for count



} // end function

</script>







</html>
3
cs_L0ci Messages postés 224 Date d'inscription vendredi 26 novembre 2010 Statut Membre Dernière intervention 11 juin 2013 7
22 mai 2012 à 13:46
Tiens plus simple, voila ma version qui fonctionne. J'ai rajouté la condition pour les espaces .
<!DOCTYPE>
<meta charset="utf-8">
<head>
<title>Essaie des ID</title>
</head>


<script type="text/javascript">

function travail()		
{	// function

var userInput = document.getElementById('userInput');
var o = document.getElementById("mondiv");
var name = userInput.value 
var consonne= 'cdfghjklmnpqrstvwxyz';
var voyelle= 'aeiou';
var result = '';

for (count=0; count<name.length; count++)  
{ // for name count

daChar = name.charCodeAt(count);

if(String.fromCharCode(daChar) != " ")
{

for (i = 0; i < consonne.length; i++) 					
{ // for consonne i

if (daChar == consonne.charCodeAt(i))			
{ // if consonne

result += ''+ String.fromCharCode(daChar) +''; 							

} // end if consonne	

}	// end for consonne

for (a = 0; a < voyelle.length; a++) 					
{ // for voyelle a

if (daChar == voyelle.charCodeAt(a))						
{ // if voyelle

result += ''+ String.fromCharCode(daChar) + ''; 

} // end if voyelle

}	// end for voyelle
}
else
{
result += " ";
}
} // end for count				
o.innerHTML = result;
} // end function

</script>





 test


</html>
1
cs_L0ci Messages postés 224 Date d'inscription vendredi 26 novembre 2010 Statut Membre Dernière intervention 11 juin 2013 7
22 mai 2012 à 10:28
Bonjour,

Alors je n'ai pas eu le temps de tester mais, les 2 premieres erreurs qui me sautent aux yeux:
-tu fais la coloration sur name et non pas sur daCHar, ce qui signifie que toute la phrase sera de la même couleur.
-il faudrait concaténer toutes les modifications dans 1 chaine et a la fin de la fin l'insérer grace a o.innerHtml
ex:
var result = "";
[...]
if (daChar == voyelle.charCodeAt(a))
{
     result += ''+ daChar + ''; 
} 
//a la fin de la fonction:
o.innerHTML = result;
0
Madonnina78 Messages postés 27 Date d'inscription vendredi 26 novembre 2010 Statut Membre Dernière intervention 9 juin 2012
22 mai 2012 à 11:10
tout d'abord merci, j'ai testé ça mais toujours rien:

<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Essaie des ID </title>


</head>



<script type="text/javascript">

function travail()

{	// function

var userInput = document.getElementById('userInput');
var o = document.getElementById("mondiv");
var name = userInput.value 
var consonne= 'cdfghjklmnpqrstvwxyz';
var voyelle= 'aeiou';


for (count=0; count<name.length; count++)  

{ // for name count

var daChar = name.charCodeAt(count);
var result = "";

for (i = 0; i < consonne.length; i++) 

{ // for consonne i

if (daChar == consonne.charCodeAt(i))

{ // if consonne

result += ''+ daChar + '';

} // end if consonne	

}	// end for consonne

for (a = 0; a < voyelle.length; a++) 

{ // for voyelle a

if (daChar == voyelle.charCodeAt(a))

{ // if voyelle

result += ''+ daChar + '';						 

} // end if voyelle				

}	// end for voyelle

} // end for count

o.innerHTML = name;

} // end function

</script>





 test


</html>
0

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

Posez votre question
Madonnina78 Messages postés 27 Date d'inscription vendredi 26 novembre 2010 Statut Membre Dernière intervention 9 juin 2012
22 mai 2012 à 11:13
pardon j'ai essayé aussi avec à la fin o.innerHTML = result; et ça me donne des chiffres.
0
cs_L0ci Messages postés 224 Date d'inscription vendredi 26 novembre 2010 Statut Membre Dernière intervention 11 juin 2013 7
22 mai 2012 à 11:30
Bonjour,

effectivement je viens de tester . Il faut utiliser
String.fromCharCode(daChar)
Il faut aussi rajouter une condition pour les espaces.

Bonen chance
0
Madonnina78 Messages postés 27 Date d'inscription vendredi 26 novembre 2010 Statut Membre Dernière intervention 9 juin 2012
22 mai 2012 à 13:18
Un grand merci ;)

En mettant CharAt au lieu de CharCode ça fonctionne mais il ne reprend dans la div que la dernière lettre de la chaine de caractère entré dans le champ userInput.

Le changement de couleur entre voyelle et consonne s'opère bien .. j'y suis presque ffff

je ne comprend pas pourquoi il reprend pas tout :( je continue à chercher...
0
cs_L0ci Messages postés 224 Date d'inscription vendredi 26 novembre 2010 Statut Membre Dernière intervention 11 juin 2013 7
22 mai 2012 à 13:30
Bonjour,

attention a bien mettre += pour concaténer dans le div:
o.innerHTML += ''+ String.fromCharCode(daChar) + ''; 
0
Madonnina78 Messages postés 27 Date d'inscription vendredi 26 novembre 2010 Statut Membre Dernière intervention 9 juin 2012
22 mai 2012 à 16:00
Oups j'avais pas vu ton dernier message je vais comparé le tien est surement mieux :p Mais encore un grand grand merci ;)
0
cs_L0ci Messages postés 224 Date d'inscription vendredi 26 novembre 2010 Statut Membre Dernière intervention 11 juin 2013 7
22 mai 2012 à 16:02
De rien. content que ca marche
0
Rejoignez-nous