Professional Documents
Culture Documents
Université Ferhat Abbas Sétif-1 2023-2024 Faculté Des Sciences, Département D'informatique Programmation Web Avancée
Université Ferhat Abbas Sétif-1 2023-2024 Faculté Des Sciences, Département D'informatique Programmation Web Avancée
Université Ferhat Abbas Sétif-1 2023-2024 Faculté Des Sciences, Département D'informatique Programmation Web Avancée
TP3
JavaScript
Array and closure.
Exercise.1
Consider the following array:
Let nums = [11, 22, 33, 46, 75, 86, 97, 98];
Write the command to remove the first element from the array.
Write the command to remove last element from the array.
Write the command to add 18 to the front of the array.
Write the command to add 75 to the end of the array.
Use the filter, map and the reduce methods to produce (use console.log to
display the results):
o An array of even numbers.
o A new array using the map function whose each element is equal to
the original element plus 4
o The sum of elements using the arrow function and then find the
average of the array
o An array of numbers divisible by 3 and then squared those
numbers.
Exercise.2
Loop through the following array to console.log a sentence describing the weather
of each day:
const weeklyForecast = [
{ day: 'monday', hi: 24, lo: 7 },
{ day: 'tuesday', hi: 23, lo: 6 },
{ day: 'wednesday', hi: 28, lo: 14 },
{ day: 'thursday', hi: 29, lo: 16 },
{ day: 'friday', hi: 22, lo: 11 },
{ day: 'saturday', hi: 21, lo: 8 },
{ day: 'sunday', hi: 26, lo: 14 },
];
For example, each sentence would look like:
On Monday, the high will be 24 degrees and the low will be 7 degrees.
Using the filter and the map methods, create a new array that only contains
the days where the high temperature is more than 23 degrees.
Exercise.3
Write a function called specialMultiply that accepts only one parameter and it
should return a function that can later passed another parameter to return the
product. You will have to use closure to solve this.
Examples:
specialMultiply(3)(4); // 12
Given an array of potential voters, return an object representing the results of the
vote. Include how many of the potential voters were in the ages 18-25, how many
from 26-35, how many from 36-55, and how many of each of those age ranges
actually voted. The resulting object containing this data should have 6 properties.
Exercise.5
Note: The Math.floor() function always rounds down and returns the largest
integer less than or equal to a given number.
Math.floor(Math.random()*10); // Returns a random integer from 0 to 9