Skip to content

Instantly share code, notes, and snippets.

@imparvez
Created September 18, 2017 12:41
Show Gist options
  • Save imparvez/542b088a2d4ebba40f3d90602a004a2a to your computer and use it in GitHub Desktop.
Save imparvez/542b088a2d4ebba40f3d90602a004a2a to your computer and use it in GitHub Desktop.
// V2 Function
// It should have a function to display todos
// It should have a function to add new todos
// It should have a function to change a todos
// It should have a function to delete a todos
var todos = ['item 1', 'item 2', 'item 3'];
// 1. It should have a function to display todos
function displayTodos(){ // Declare a function
console.log('My Todos: ', todos);
}
displayTodos(); // Call a function
// 2. It should have a function to add new todos
function addTodos(todo){ // Declare a function which will take a single parameter
todos.push(todo);
displayTodos(); // function inside another function
}
addTodos('item 4'); // Call a function with a new todo
addTodos('item 5');
// 3. It should have a function to change a todos
function changeTodo(pos, newTodo){
todos[pos] = newTodo;
displayTodos();
}
changeTodo(0, 'item 1 updated');
// 4. It should have a function to delete a todos
function deleteTodo(pos){
todos.splice(pos,1);
displayTodos();
}
deleteTodo(0); // ["item 2", "item 3", "item 4", "item 5"]
deleteTodo(2); // ["item 2", "item 3", "item 5"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment