Javascript : Alerte tout en un.

Résolu
huriddin Messages postés 13 Date d'inscription   Statut Membre Dernière intervention   -  
huriddin Messages postés 13 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour, aujourd'hui je suis bloqué sur un petit problème en Javascript.

En effet, le code ci dessous fourni des informations sur les personnages mais chacun sur des alertes boxes différents, comment tout regroupé sur un seul et unique alerte box ?

voilà le code :

window.onload = function() {

function Element(name,age,country){
this.name = name;
this.age = age;
this.country = country;

this.totalk = function() {
return this.name + ' is from ' + this.country;
}

this.sayname = function(number) {

if(person[number].country == 'Switzerland' && this.age > 17) {
return this.name + ' is ' + this.age + ' years old, (s)he can smoke. (' + this.country + ' -> minimum 18)';
} else if (person[number].country == 'USA' && this.age > 20) {
return this.name + ' is ' + this.age + ' years old, (s)he can smoke. (' + this.country + ' -> minimum 21)';
} else {
return this.name + ' is ' + this.age + ' years old, that\'s why, (s)he should not smoke. (minimum 21)';
}
}
};

var person = new Array();
person[0] = new Element('Johnny', 18, 'England');
person[1] = new Element('Mercedes', 23, 'USA');
person[2] = new Element('Bastien', 21, 'Switzerland');
person[3] = new Element('Jimmy', 46, 'USA');
person[4] = new Element('Rodriguez', 16, 'Portugal');



for(var key in person) {
alert(person[key].totalk() + ' AND ' + person[key].sayname(key));
}

};
A voir également:

1 réponse

Nexii Messages postés 338 Date d'inscription   Statut Membre Dernière intervention   588
 
var chaine = null;

for(var key in person) {
        // \n pour un saut de ligne
        chaine += person[key].totalk() + ' AND ' + person[key].sayname(key) + ' \n ';
}
alert(chaine);

1
huriddin Messages postés 13 Date d'inscription   Statut Membre Dernière intervention  
 
Merci !
0