Vous êtes sur la page 1sur 3

Examen final : Langage Javascript

Exercice 1

unction incrementer(nombre) {

return nombre + 1;

// Exemple d'utilisation :

const nombreInitial = 5;

const resultat = incrementer(nombreInitial);

console.log(resultat); // Affiche 6

Exercice 2

function verifierPairImpair(nombre) {

if (nombre % 2 === 0) {

return "pair";

} else {

return "impair";

// Exemple d'utilisation :

const nombre1 = 4;

const resultat1 = verifierPairImpair(nombre1);

console.log(resultat1); // Affiche "pair"

const nombre2 = 7;

const resultat2 = verifierPairImpair(nombre2);

console.log(resultat2); // Affiche "impair"

Exercice 3
function helloWorld(number) {

if (number % 3 === 0 && number % 5 === 0) {

return "Hello World";

} else if (number % 3 === 0) {

return "Hello";

} else if (number % 5 === 0) {

return "World";

} else {

return number.toString();

// Exemples d'utilisation :

console.log(helloWorld(9)); // Affiche "Hello"

console.log(helloWorld(25)); // Affiche "World"

console.log(helloWorld(15)); // Affiche "Hello World"

console.log(helloWorld(7)); // Affiche "7

Exercice 4

function sumArray(array) {

let sum = 0;

for (let i = 0; i < array.length; i++) {

if (typeof array[i] === 'number') {

sum += array[i];

return sum;

// Exemples d'utilisation :

console.log(sumArray([1, 2, 3, 4, 5])); // Affiche 15 (1 + 2 + 3 + 4 + 5)


console.log(sumArray([-2, 10, 7, 3])); // Affiche 18 (-2 + 10 + 7 + 3)

console.log(sumArray([5, "Hello", 8, 2])); // Affiche 15 (5 + 8 + 2)

console.log(sumArray([])); // Affiche 0 (tableau vide)

Exercice 5

function trierBoissonsParPrix(boissons) {

// Trie le tableau des boissons par prix dans l'ordre croissant

boissons.sort((a, b) => a.prix - b.prix);

// Utilise la fonction map pour extraire les noms triés des boissons

const nomsTriés = boissons.map(boisson => boisson.nom);

return nomsTriés;

// Exemple d'utilisation avec le tableau donné dans la question

const boissons = [

{ nom: "citron", prix: 50 },

{ nom: "menthe", prix: 35 },

{ nom: "fraise", prix: 5 }

];

const nomsTriés = trierBoissonsParPrix(boissons);

console.log(nomsTriés); // Affiche ["fraise", "menthe", "citron"]

Vous aimerez peut-être aussi