0% found this document useful (0 votes)
16 views2 pages

tp4 Refactorisation

The document describes a small ToDo application with functions to add tasks, delete tasks, and display tasks. It includes sample code to demonstrate adding two tasks, displaying all tasks, deleting the first task, and displaying the remaining tasks.

Uploaded by

juliamolokovych
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)
16 views2 pages

tp4 Refactorisation

The document describes a small ToDo application with functions to add tasks, delete tasks, and display tasks. It includes sample code to demonstrate adding two tasks, displaying all tasks, deleting the first task, and displaying the remaining tasks.

Uploaded by

juliamolokovych
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
You are on page 1/ 2

TP 4 refactorisation

Imaginons que nous avons un petit système pour gérer des tâches dans une application ToDo. Le
code ci-dessous comprend des fonctions pour ajouter une tâche, supprimer une tâche, et afficher
toutes les tâches.

let tasks = [];

function addTask(taskName, priority = 'normal') {


let task = {
id: tasks.length + 1,// Auto-incrementation
name: taskName,
priority: priority,
completed: false
};
tasks.push(task);
console.log(`Task "${taskName}" added successfully.`);
}

function deleteTask(taskId) {
let index = -1;
for (let i = 0; i < tasks.length; i++) {
if (tasks[i].id === taskId) {
index = i;
break;
}
}
if (index !== -1) {
tasks.splice(index, 1);
console.log(`Task with id ${taskId} deleted successfully.`);
} else {
console.log(`Task with id ${taskId} not found.`);
}
}

function displayTasks() {
if (tasks.length === 0) {
console.log("No tasks to display.");
return;
}
console.log("Displaying all tasks:");
for (let i = 0; i < tasks.length; i++) {
let task = tasks[i];
console.log(`Id: ${task.id}, Name: ${task.name}, Priority:
${task.priority}, Completed: ${task.completed}`);
}
}

// Exemple d'utilisation
addTask("Learn JavaScript", "high");
addTask("Read a book");
displayTasks();
deleteTask(1);
displayTasks();

You might also like