-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Array Home work #2
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,119 @@ | ||
function homework (renderList) { | ||
// ❌ DO NOT CHANGE THE NAMES OF THE FUNCTIONS (eg. onAddTodo) | ||
// DO NOT REMOVE renderList(todos); FROM THE END OF THE FUNCTIONS | ||
|
||
// 👇 ONLY EDIT BELOW THIS LINE 👇 | ||
|
||
// The todos when you first load the page | ||
let todos = [ | ||
'buy milk', | ||
'go to gym', | ||
'do CYF homework', | ||
]; | ||
|
||
// ✅This function should: | ||
// ◽ remove all of the todo items | ||
function onClearAll () { | ||
// Add code here | ||
|
||
renderList(todos); | ||
} | ||
|
||
// ✅This function should: | ||
// ◽ sort the array of todos alphabetically | ||
function onSort () { | ||
// Add code here | ||
|
||
renderList(todos); | ||
} | ||
|
||
// ✅This function should: | ||
// ◽ reverse the order of the todos in the array | ||
function onReverse () { | ||
// Add code here | ||
|
||
renderList(todos); | ||
} | ||
|
||
// ✅This function should: | ||
// ◽ add a new item to the end of the todos array | ||
// ◽ bonus: do not allow new items to be less than 3 characters long | ||
function onAddTodo (newTodoText) { | ||
// Add code here | ||
|
||
renderList(todos); | ||
} | ||
|
||
// ✅This function should: | ||
// ◽ remove the item at the selected index | ||
function onRemoveTodo (index) { | ||
// Add code here | ||
|
||
renderList(todos); | ||
} | ||
|
||
// ✅This function should: | ||
// ◽ add a capital X to the start of the item ('buy milk' => 'X buy milk') | ||
// ◽ bonus: don't add an X if it already has one (it's already complete) | ||
// ◽ super bonus: remove the X if it already has one (put it back to incomplete) | ||
function onCompleteTodo (index) { | ||
// Add code here | ||
|
||
renderList(todos); | ||
} | ||
|
||
// ✅This function should: | ||
// ◽ remove from the list all of the completed items (that start with 'X') | ||
function onRemoveCompleted () { | ||
// Add code here | ||
|
||
renderList(todos); | ||
} | ||
|
||
// ✅This function should: | ||
// ◽ add an exclamation mark ('!') to the end of every item in the list | ||
function onUrgent () { | ||
// Add code here | ||
|
||
renderList(todos); | ||
} | ||
|
||
// ✅This function should: | ||
// ◽ display an alert with the first item on the list not marked as done (first without an 'X') | ||
// ◽ bonus: consider what will happen if there are no more items left to do | ||
function onWhatNext () { | ||
alert('This is just a dummy message!'); | ||
} | ||
|
||
// ☝ ONLY EDIT ABOVE THIS LINE ☝ | ||
renderList(todos); | ||
return { | ||
onAddTodo, onRemoveTodo, onCompleteTodo, onClearAll, onSort, onReverse, onRemoveCompleted, onUrgent, onWhatNext | ||
}; | ||
} | ||
function homework(renderList) { | ||
// ❌ DO NOT CHANGE THE NAMES OF THE FUNCTIONS (eg. onAddTodo) | ||
// DO NOT REMOVE renderList(todos); FROM THE END OF THE FUNCTIONS | ||
|
||
// 👇 ONLY EDIT BELOW THIS LINE 👇 | ||
|
||
// The todos when you first load the page | ||
let todos = ["buy milk", "go to gym", "do CYF homework"]; | ||
|
||
// ✅This function should: | ||
// ◽ remove all of the todo items | ||
function onClearAll() { | ||
todos.length = 0; // we can Set the array length to zero | ||
// or we can replace an array with an empty array todos = []; | ||
|
||
renderList(todos); | ||
} | ||
|
||
// ✅This function should: | ||
// ◽ sort the array of todos alphabetically | ||
function onSort() { | ||
todos.sort(); // Add code here | ||
|
||
renderList(todos); | ||
} | ||
|
||
// ✅This function should: | ||
// ◽ reverse the order of the todos in the array | ||
function onReverse() { | ||
todos.reverse(); // Add code here | ||
|
||
renderList(todos); | ||
} | ||
|
||
// ✅This function should: | ||
// ◽ add a new item to the end of the todos array | ||
// ◽ bonus: do not allow new items to be less than 3 characters long | ||
function onAddTodo(newTodoText) { | ||
// Add code here | ||
|
||
if (newTodoText.length < 4) { | ||
//process.exit(1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think by now you must have discovered that this doesn't work in the browser. There are a number of differences between Node and web browser JS, as you'll find out later in the course. |
||
alert(newTodoText + " It should be more than 3 characters long "); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that you've used the alert function from later down in the exercise to solve a problem here too. |
||
} else { | ||
todos.push(newTodoText); | ||
} | ||
renderList(todos); | ||
} | ||
|
||
// ✅This function should: | ||
// ◽ remove the item at the selected index | ||
function onRemoveTodo(index) { | ||
// Add code here | ||
todos.splice(index, 1); | ||
renderList(todos); | ||
} | ||
|
||
// ✅This function should: | ||
// ◽ add a capital X to the start of the item ('buy milk' => 'X buy milk') | ||
// ◽ bonus: don't add an X if it already has one (it's already complete) | ||
// ◽ super bonus: remove the X if it already has one (put it back to incomplete) | ||
function onCompleteTodo(index) { | ||
// Add code here | ||
if (todos[index].startsWith("X")) { | ||
todos[index] = todos[index].slice(1); | ||
} else { | ||
todos[index] = "X " + todos[index]; | ||
} //I think there will be a problem that we can't add new todo that starts with X because it | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Absolutely right! It's always good to consider the unintended consequences of what you've been asked to do. Do you have any ideas for how we could make this better? |
||
// will always consider it complete . | ||
|
||
renderList(todos); | ||
} | ||
|
||
// ✅This function should: | ||
// ◽ remove from the list all of the completed items (that start with 'X') | ||
function onRemoveCompleted() { | ||
// Add code here | ||
todos = todos.filter(function(item) { | ||
return !item.startsWith("X"); | ||
}); | ||
renderList(todos); | ||
} | ||
|
||
// ✅This function should: | ||
// ◽ add an exclamation mark ('!') to the end of every item in the list | ||
function onUrgent() { | ||
todos = todos.map(function(item) { | ||
return item + "!"; | ||
}); // Add code here | ||
console.log(todos); | ||
renderList(todos); | ||
} | ||
|
||
// ✅This function should: | ||
// ◽ display an alert with the first item on the list not marked as done (first without an 'X') | ||
// ◽ bonus: consider what will happen if there are no more items left to do :find will returns | ||
// undefined. so the alert will return undefined . | ||
function onWhatNext() { | ||
alert( | ||
todos.find(function(item) { | ||
return !item.startsWith("X"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the third time you have checked that a string starts with an |
||
}) | ||
); | ||
} | ||
|
||
// ☝ ONLY EDIT ABOVE THIS LINE ☝ | ||
renderList(todos); | ||
return { | ||
onAddTodo, | ||
onRemoveTodo, | ||
onCompleteTodo, | ||
onClearAll, | ||
onSort, | ||
onReverse, | ||
onRemoveCompleted, | ||
onUrgent, | ||
onWhatNext | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's good that you've considered multiple solutions!