todo.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const todos = [{
  2. text: 'Order Cat food',
  3. completed: false
  4. }, {
  5. text: 'Clean kitchen',
  6. completed: true
  7. }, {
  8. text: 'Buy food',
  9. completed: true
  10. }, {
  11. text: 'Do work',
  12. completed: false
  13. }, {
  14. text: 'Exercise',
  15. completed: true
  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)
  42. // false 0 < true 1 returns true
  43. // true 1 < false 0 returns false
  44. // false should be top, true will be on bottom
  45. //
  46. const sortTodos = function(todos) {
  47. todos.sort(function(a, b) {
  48. if (!a.completed && b.completed ) {
  49. return -1
  50. } else {
  51. if (!b.completed && a.completed) {
  52. return 1
  53. } else {
  54. return 0
  55. }
  56. }
  57. })
  58. }
  59. sortTodos(todos)
  60. console.log(todos)