https://docs.google.com/document/d/1onP6XUG7DX_ZwwiUXDjGbW2VL2KLJDdQpWa-ofwYWMo/edit?usp=sharing
🥷DM : Array
...
🍕Base des données
const pizzas = [
{ name:"queen", ingredients: ["🐷","🍄","🍅","🧀"] },
{ name: "cheese", ingredients: ["🧀", "🍅"]},
{ name: "oriental", ingredients: ["🍅","🐑","🍄","🌶"]},
{ name: "royal", ingredients: ["🍅","🌵"]},
];
🪛 Questions
Trouvez les pizzas avec du 🧀
Trouvez les pizzas avec du 🍅
Trouvez les pizzas avec un ingrédient quelconque (soit du "🍅" ou du "🧀" ou du "🐑" ...)
Trouvez les pizzas avec au moins un ingrédient parmi une liste d'ingrédients
Trouvez les pizzas avec tous les ingrédients d'une liste
Trouvez les pizzas avec viande
Ajoutez pour chaque pizza le type (🐮ou ☘️)
Trouvez l'ensemble des ingrédients "🐷","🍄","🍅","🧀","🐑","🌶","🌵"
Trouvez pour chaque ingrédient le nombre de pizzas l'utilisant ,"🧀":2 …
Trouvez pour chaque ingrédient la liste des pizzas l'utilisant
Ajoutez le prix de chaque pizza
prices = new Map([ ["🍅", 1], ["🐷", 2], ["🌶",2], ["🍄", 5], ["🧀", 5], ["🐑", 2], ["🌵", 10]]);
Cours Array
Objectif du cours
1. **Function Style, Method Style**
The main difference between the function style and the use of the `filter()` method lies in the programming paradigm they represent.
1. **Function Style**: The function style represents the procedural programming paradigm. In this style, you write a sequence of commands for the computer to perform. The function `filterArray` that we wrote earlier is an example of this. It uses a `for` loop to iterate over the array and an `if` statement to check each element against the test function. This style gives you more control over the details of how the array is processed.
2. **Method Style**:
The method style represents the functional programming paradigm. In this style, you use built-in array methods like `filter()` to process the array. The `filter()` method abstracts away the details of how the array is processed, allowing you to focus on what you want to do (filter the array) rather than how to do it. This style can lead to more concise and readable code.
In general, the method style is considered more "modern" and is often preferred in JavaScript, but both styles have their uses and can be appropriate in different situations.
Test
Pour tester le code définisez un tableau de nombres.
lien : https://javascript.info/array-methods
Cours
TD arbre
Cours parcours du DOM
👿Test
🪛L'idée est de parcourir le DOM à la recherche d'un item contenant un texte.
Si le nœud contient ce string on met le nœud en rouge.
Nous allons nous inspirer de la structure du code récursif pour parcourir le DOM
printDOM = (node, prefix) => {
//action
for(let nd of node.children) {
printDOM ( nd, prefix + '-' );
}
}
printDOM(document," ");
Remplaçons simplement action par :
if (node.innerHTML.indexOf(string) > -1) {
node.style.color="red";}
🆘Ce code met en rouge le noeud node contenant le mot string.
Bilan
function find(node, string) {
if (node.innerHTML.indexOf(string) > -1) {
node.style.color="red";
}
for (n of node.children) {
find(n, string);
}
}
console.log(find(document.body, "para"));
👿Testez le code ;
Passons à un code offrant une plus grande granularité sur les nœuds
code
Trouver enfin un dernier bug !
function find(node, string) {
if (node.nodeType == document.ELEMENT_NODE && node.nodeName !=
"SCRIPT") {
for (var i = 0; i < node.childNodes.length; i++) {
if (find(node.childNodes[i], string))
return true;
}
return false;
} else if (node.nodeType == document.TEXT_NODE) {
if (node.nodeValue.indexOf(string) > -1) {
node.style.color="red"; // ou node.classList.add("find");
return true;
}
return false;
}
}
find(document.body, "second");
🆘idée !
DS :
🪛On désire par courir le DOM pour mettre en rouge le paragraphe contenant "second".
See the Pen find bug by dupont (@dupontcodepen) on CodePen.
Code to use
Clonez le code
https://github.com/dupontdenis/Cours-L2-method.git
Modifiez le fichier test/yourCode.mjs