Parcourir la source

finish array sort challenge

Signed-off-by: Michael Tang <michael.h.tang@gmail.com>
Michael Tang il y a 5 ans
Parent
commit
4ddcaf9056
2 fichiers modifiés avec 46 ajouts et 7 suppressions
  1. 19 2
      notes.js
  2. 27 5
      todo.js

+ 19 - 2
notes.js

@@ -18,6 +18,20 @@ const notes = [{
 //     return notes[index]
 // }
 
+
+
+const sortNotes = function (notes) {
+    notes.sort(function (a, b){
+        if (a.title.toLowerCase() < b.title.toLowerCase()) {
+            return -1
+        } else if (b.title.toLowerCase() < a.title.toLowerCase()) {
+            return 1
+        } else {
+            return 0
+        }
+    })
+}
+
 const findNote = function (notes, noteTitle) {
     return notes.find(function (note, index) {
         let currentTitle = String(note.title).toLowerCase()
@@ -37,7 +51,7 @@ const findNotes = function (notes, query){
     
  }
 
-console.log(findNotes(notes, 'eating')) 
+// console.log(findNotes(notes, 'eating')) 
 
 // const note = findNote(notes, 'office other modifications')
 // console.log(note)
@@ -57,4 +71,7 @@ console.log(findNotes(notes, 'eating'))
 //     return note.title === 'Habits to work on'
 // })
 
-// console.log(index)
+// console.log(index)
+
+sortNotes(notes)
+console.log(notes)

+ 27 - 5
todo.js

@@ -1,18 +1,18 @@
 const todos = [{
     text: 'Order Cat food',
-    completed: true
+    completed: false
 }, {
     text: 'Clean kitchen',
     completed: true
 }, {
     text: 'Buy food',
-    completed: false
+    completed: true
 }, {
     text: 'Do work',
     completed: false
 }, {
     text: 'Exercise',
-    completed: false
+    completed: true
 }]
 
 // 1. Convert array to array of objects -> text, completed
@@ -40,7 +40,29 @@ const getThingsToDo = function (todos) {
     })
 }
 
-console.log(getThingsToDo(todos))
+// console.log(getThingsToDo(todos))
+
+// deleteTodo(todos, 'buy food')
+// console.log(todos)
+
+// false 0 < true 1 returns true
+// true 1 < false 0 returns false
+
+// false should be top, true will be on bottom
+//  
+const sortTodos = function(todos) {
+    todos.sort(function(a, b) {
+        if (!a.completed && b.completed ) {
+            return -1
+        } else {
+            if (!b.completed && a.completed) {
+                return 1
+            } else {
+                return 0
+            }
+        }
+    })
+}
 
-deleteTodo(todos, 'buy food')
+sortTodos(todos)
 console.log(todos)