Javascript : Alerte tout en un.

Résolu
huriddin Messages postés 16 Statut Membre -  
huriddin Messages postés 16 Statut Membre -
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));
}

};

1 réponse

  1. Nexii Messages postés 365 Statut Membre 591
     
    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
    1. huriddin Messages postés 16 Statut Membre
       
      Merci !
      0