Dernière mise à jour : 26/05/2016
Créez une fonction qui fait la somme d'un nombre indéterminé d'entiers. Écrivez un script qui va tester cette fonction.
function sumAll() { var index; var sum = 0; for(index = 0; index < arguments.length; ++index) { sum += arguments[index]; } return sum; } var a = 12; var b = 34; var c = 56; var d = 78; var e = 90; document.writeln("Variables définies:"); document.writeln("-------------------"); document.writeln("a = "+a); document.writeln("b = "+b); document.writeln("c = "+c); document.writeln("d = "+d); document.writeln("e = "+e); document.writeln("") document.writeln("Additions:"); document.writeln("----------"); document.writeln("Somme de a ("+a+"), b ("+b+") et c ("+c+") : sumAll(a, b, c) => "+sumAll(a, b, c)); document.writeln("Somme de b ("+b+") et d ("+d+") : sumAll(b, d) => "+sumAll(b, d)); document.writeln("Somme de a ("+a+"), b ("+b+"), c ("+c+"), d ("+d+") et e ("+e+") : sumAll(a, b, c, d, e) => "+sumAll(a, b, c, d, e)); document.writeln("Somme d'aucun élément : sumAll() => "+sumAll());
<!DOCTYPE html> <html lang="fr"> <head> <meta author="Sébastien Adam"> <meta charset="UTF-8"> <meta name="language" content="fr"> <title>Fonction avec un nombre indéterminé d'arguments</title> </head> <body> <pre> <script type="text/javascript" src="js/script.js"></script> </pre> </body> </html>
© Sébastien Adam 1996 ~ 2024 - https://www.sebastienadam.be/ - .