Université Ferhat Abbas Sétif-1 2023-2024 Faculté Des Sciences, Département D'informatique Programmation Web Avancée

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

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

Exercise.4 (will be solved in the course)

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

Write a function called guessingGame, which takes in one parameter amount.


The function should return another function that takes in a parameter called
guess. In the outer function, you should create a variable called answer, which
is the result of a random number between 0 and 10 as well as a variable called
guesses that should be set to zero.
In the inner function, if the guess passed in is the same as the random number
(defined in the outer function) - you should return the string "You got it!". If the
guess is too high return "Your guess is too high!" and if it is too low, return
"Your guess is too low!". You should stop the user from guessing if the amount
of guesses they have made is greater than the initial amount passed to the outer
function.
Examples:
var game = guessingGame(5)
game(1) // "You're too low!"
game(8) // "You're too high!"
game(5) // "You're too low!"
game(7) // "You got it!"
game(1) // "You are all done playing!"

var game2 = guessingGame(3)


game2(5) // "You're too low!"
game2(3) // "You're too low!"
game2(1) // "No more guesses the answer was 0"
game2(1) // "You are all done playing!"

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

You might also like