Thursday, June 5, 2025
JAVA SCRIPT CODING
// Simple Task Tracker in JavaScript
// Create an empty array to store tasks
let tasks = [];
// Function to add a new task
function addTask(description) {
if (!description || typeof description !== 'string') {
console.log('Invalid task description.');
return;
}
const newTask = {
id: Date.now(), // Unique ID based on timestamp
description: description.trim(),
completed: false
};
tasks.push(newTask);
console.log(`Task added: "${newTask.description}"`);
}
// Function to remove a task by ID
function removeTask(taskId) {
const initialLength = tasks.length;
tasks = tasks.filter(task => task.id !== taskId);
if (tasks.length < initialLength) {
console.log(`Task with ID ${taskId} removed.`);
} else {
console.log(`No task found with ID ${taskId}.`);
}
}
// Function to toggle completion status of a task
function toggleTask(taskId) {
const task = tasks.find(task => task.id === taskId);
if (task) {
task.completed = !task.completed;
console.log(`Task "${task.description}" marked as ${task.completed ? 'completed' : 'incomplete'}.`);
} else {
console.log(`Task with ID ${taskId} not found.`);
}
}
// Function to list all tasks
function listTasks() {
if (tasks.length === 0) {
console.log('No tasks to show.');
return;
}
console.log('\nCurrent Tasks:');
tasks.forEach(task => {
const status = task.completed ? '✅' : '⬜';
console.log(`${status} [${task.id}] ${task.description}`);
});
}
// Example usage:
addTask('Learn JavaScript');
addTask('Build a small project');
addTask('Write clean code');
listTasks();
// Toggle the second task (you would use the correct ID in real use)
if (tasks[1]) toggleTask(tasks[1].id);
// Remove the first task
if (tasks[0]) removeTask(tasks[0].id);
// List updated tasks
listTasks();
Subscribe to:
Post Comments (Atom)
Components breadcrumb Examples Structure in Bootstrap
Breadcrumb Examples: The Bootstrap breadcrumb examples framework shows how this navigation ...
-
XML Coding: The versatile and popular eXtensible Markup Language, or XML for short, is made to store and transfer d...
-
In CSS, the term “tags” is commonly used by beginners, but the correct term is selectors . CSS selectors are one of the most important p...
No comments:
Post a Comment