todo.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const todos = [{
  2. text: 'Order Cat food',
  3. completed: true
  4. }, {
  5. text: 'Clean kitchen',
  6. completed: true
  7. }, {
  8. text: 'Buy food',
  9. completed: false
  10. }, {
  11. text: 'Do work',
  12. completed: false
  13. }, {
  14. text: 'Exercise',
  15. completed: false
  16. }]
  17. // 1. Convert array to array of objects -> text, completed
  18. // 2. Create function to remove a todo by text value
  19. const deleteTodo = function (todos, todoText) {
  20. const index = todos.findIndex(function(array) {
  21. if (array.text.toLowerCase() === todoText.toLowerCase()){
  22. console.log(`found!`)
  23. }
  24. console.log(`not found!`)
  25. return array.text.toLowerCase() === todoText.toLowerCase()
  26. })
  27. // console.log(index)
  28. if (index > -1) {
  29. todos.splice(index, 1)
  30. console.log(`deleted index at ${index}`)
  31. }
  32. }
  33. // returns only the items that are set to completed: false
  34. const getThingsToDo = function (todos) {
  35. return todos.filter(function (todo, index) {
  36. return !todo.completed
  37. })
  38. }
  39. console.log(getThingsToDo(todos))
  40. deleteTodo(todos, 'buy food')
  41. console.log(todos)