Javascript : Alerte tout en un.

Résolu/Fermé
huriddin Messages postés 13 Date d'inscription lundi 30 juin 2014 Statut Membre Dernière intervention 13 juillet 2014 - 2 juil. 2014 à 13:38
huriddin Messages postés 13 Date d'inscription lundi 30 juin 2014 Statut Membre Dernière intervention 13 juillet 2014 - 2 juil. 2014 à 14:36
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 jeudi 13 mars 2014 Statut Membre Dernière intervention 14 mars 2017 585
Modifié par Nexii le 2/07/2014 à 13:49
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 lundi 30 juin 2014 Statut Membre Dernière intervention 13 juillet 2014
2 juil. 2014 à 14:36
Merci !
0