thomas
This commit is contained in:
25
TP1/EX3/array.html
Normal file
25
TP1/EX3/array.html
Normal file
@@ -0,0 +1,25 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Array Operations</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<script>
|
||||
const numbers = [1, 5, 2, 8, 3, 7, 4, 6];
|
||||
|
||||
// Utilisation de map pour créer un nouveau tableau avec chaque nombre doublé
|
||||
const doubledNumbers = numbers.map(num => num * 2);
|
||||
console.log("Doubled numbers:", doubledNumbers); // [2, 10, 4, 16, 6, 14, 8, 12]
|
||||
|
||||
// Utilisation de filter pour garder seulement les nombres pairs
|
||||
const evenNumbers = numbers.filter(num => num % 2 === 0);
|
||||
console.log("Even numbers:", evenNumbers); // [2, 8, 4, 6]
|
||||
|
||||
// Utilisation de reduce pour calculer la somme de tous les nombres
|
||||
const sum = numbers.reduce((total, num) => total + num, 0);
|
||||
console.log("Sum:", sum); // 36
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
31
TP1/EX3/minmax.html
Normal file
31
TP1/EX3/minmax.html
Normal file
@@ -0,0 +1,31 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Min/Max with Reduce</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<script>
|
||||
function minmax(array) {
|
||||
if (array.length === 0) {
|
||||
return { min: undefined, max: undefined };
|
||||
}
|
||||
|
||||
return array.reduce(
|
||||
(acc, val) => {
|
||||
return {
|
||||
min: Math.min(acc.min, val),
|
||||
max: Math.max(acc.max, val)
|
||||
};
|
||||
},
|
||||
{ min: array[0], max: array[0] }
|
||||
);
|
||||
}
|
||||
|
||||
const data = [10, 5, 20, 3, 15];
|
||||
const result = minmax(data);
|
||||
console.log("Min/Max:", result); // { min: 3, max: 20 }
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user