0% found this document useful (0 votes)
10 views4 pages

JavaScript Exercises for Beginners

The document contains a series of JavaScript exercises demonstrating various programming concepts such as variable scope, function definitions, array methods, promises, and object destructuring. Each exercise includes code snippets and expected output, illustrating how to manipulate data and handle asynchronous operations. The exercises cover fundamental JavaScript features, making it a useful resource for learning the language.

Uploaded by

yassine atiki
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

JavaScript Exercises for Beginners

The document contains a series of JavaScript exercises demonstrating various programming concepts such as variable scope, function definitions, array methods, promises, and object destructuring. Each exercise includes code snippets and expected output, illustrating how to manipulate data and handle asynchronous operations. The exercises cover fundamental JavaScript features, making it a useful resource for learning the language.

Uploaded by

yassine atiki
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

// YASSINE ATIKI G3

// Exercice 1

var x = 5;

let y = 10;

const z = 15;

x = 20;

y = 30;

[Link](x, y, z); // 20 30 15

// Exercice 2

function testScope() {

if (true) {

var a = "var visible partout";

let b = "let visible ici seulement";

[Link](a); // var visible partout

// [Link](b); // ReferenceError

testScope();

// Exercice 3

function sayHello(name) {

return `Bonjour ${name}`;

const sayHelloArrow = (name) => `Bonjour ${name}`;

[Link](sayHello("Ali")); // Bonjour Ali

[Link](sayHelloArrow("Sara")); // Bonjour Sara

// Exercice 4

const person = {
name: "Sara",

sayHello: function () {

[Link]("Bonjour " + [Link]);

},

sayHelloArrow: () => {

[Link]("Bonjour " + [Link]);

},

};

[Link](); // Bonjour Sara

[Link](); // Bonjour undefined

// Exercice 5

const fruits = ["pomme", "banane", "orange"];

[Link]("kiwi");

[Link]();

[Link](fruits); // ["pomme", "banane", "orange"]

// Exercice 6

const nums = [1, 2, 3, 4, 5];

[Link]([Link](n => n * 2)); // [2, 4, 6, 8, 10]

[Link]([Link](n => n % 2 === 0)); // [2, 4]

[Link]([Link]((sum, n) => sum + n, 0)); // 15

// Exercice 7

[Link]([Link](n => n > 3)); // 4

[Link]([Link](n => n < 0)); // false

[Link]([Link](n => n > 0)); // true

// Exercice 8

const user = { id: 1, name: "Ali", city: "Rabat" };

const { name, city } = user;


[Link](`${name} habite à ${city}`); // Ali habite à Rabat

const { name: fullName, ...rest } = user;

[Link](fullName); // Ali

[Link](rest); // { id: 1, city: "Rabat" }

// Exercice 9

const p = new Promise((resolve) => {

setTimeout(() => resolve("Opération terminée !"), 2000);

});

[Link](result => [Link](result)); // (après 2s) "Opération terminée !"

// Exercice 10

async function getUsers() {

try {

const res = await fetch("[Link]

const data = await [Link]();

[Link](data); // Tableau d'utilisateurs

} catch (e) {

[Link]("Erreur :", [Link]);

getUsers();

// Exercice 11

const name2 = "Nadia";

const hour = new Date().getHours();

[Link](`Bonjour ${name2}, il est ${hour}h`); // Bonjour Nadia, il est (heure actuelle)h

// Exercice 12

const arr1 = [1, 2];

const arr2 = [...arr1, 3, 4];


[Link](arr2); // [1, 2, 3, 4]

function sum(...numbers) {

return [Link]((a, b) => a + b, 0);

[Link](sum(1, 2, 3, 4)); // 10

// Exercice 13

const settings = { theme: null };

[Link]([Link] ?? "light"); // light

const user2 = { profile: { email: "x@[Link]" } };

[Link]([Link]?.email); // x@[Link]

[Link]([Link]?.city); // undefined

// Exercice 14

const produits = [

{ nom: "Lait", prix: 10, expireLe: "2025-12-01" },

{ nom: "Yaourt", prix: 5, expireLe: "2024-01-01" },

{ nom: "Jus", prix: 8, expireLe: "2026-02-15" },

];

const aujourdHui = new Date();

const valides = [Link](p => new Date([Link]) > aujourdHui);

const total = [Link]((s, p) => s + [Link], 0);

[Link]("Produits valides :", valides); // [{Lait...}, {Jus...}]

[Link]("Total :", total, "DH"); // Total : 18 DH

You might also like