Procházet zdrojové kódy

Finish filtering arrays

Signed-off-by: Michael Tang <michael.h.tang@gmail.com>
Michael Tang před 5 roky
rodič
revize
605f37eb0a
2 změnil soubory, kde provedl 101 přidání a 39 odebrání
  1. 58 25
      notes.js
  2. 43 14
      todo.js

+ 58 - 25
notes.js

@@ -1,27 +1,60 @@
-const notes = ['Note 1', 'Note 2', 'Note 3']
-
-// console.log(notes.pop())
-// notes.push('My new note')
-
-// console.log(notes.shift())
-// notes.unshift('My first note')
-
-// notes.splice(1, 1, 'This is the new second item')
-
-notes[2] = 'This is the new note 3'
-
-notes.forEach(function (item, index) {
-    console.log(index)
-    console.log(item)
-})
-
-console.log(notes.length)
-console.log(notes)
-
-for (let count = 0; count <= 2; count++) {
-    console.log(count)
+const notes = [{
+    title: 'My next trip',
+    body: 'I would like to go to Spain'
+}, {
+    title: 'Habits to work on',
+    body: 'excercise. eating a bit better'
+}, {
+    title: 'Office modifications',
+    body: 'Set up table'
+}]
+
+// const findNote = function (notes, noteTitle) {
+//     const index = notes.findIndex(function (note, index) {
+//         let currentTitle = String(note.title).toLowerCase()
+//         console.log(currentTitle)
+//         return currentTitle === noteTitle.toLowerCase()
+//     })
+//     return notes[index]
+// }
+
+const findNote = function (notes, noteTitle) {
+    return notes.find(function (note, index) {
+        let currentTitle = String(note.title).toLowerCase()
+        console.log(currentTitle)
+        return currentTitle === noteTitle.toLowerCase()
+    })
 }
 
-for (let count = 0; count < notes.length; count++) {
-    console.log(notes[count])
-}
+const findNotes = function (notes, query){
+    return notes.filter(function (note, index) {
+        const isTitleMatch = note.title.toLowerCase().includes(query.toLowerCase())
+        const isBodyMatch = note.body.toLowerCase().includes(query.toLowerCase())
+        // const isTitleMatch = String(note.title).toLowerCase().includes('ne')
+        // const isBodyMatch = String(note.body).toLowerCase().includes('ne')    
+        return isTitleMatch || isBodyMatch
+    })
+    
+ }
+
+console.log(findNotes(notes, 'eating')) 
+
+// const note = findNote(notes, 'office other modifications')
+// console.log(note)
+// console.log(notes.length)
+// console.log(notes)
+
+// for (let count = 0; count <= 2; count++) {
+//     console.log(count)
+// }
+
+// for (let count = 0; count < notes.length; count++) {
+//     console.log(notes[count])
+// }
+
+// const index = notes.findIndex(function (note, index) {
+//     console.log(note)
+//     return note.title === 'Habits to work on'
+// })
+
+// console.log(index)

+ 43 - 14
todo.js

@@ -1,17 +1,46 @@
-const todo = ['Call with Jon', "Return Mom's trimmer", 'Get Insurance', 'Register for wcb', 'Wake up for christmas']
+const todos = [{
+    text: 'Order Cat food',
+    completed: true
+}, {
+    text: 'Clean kitchen',
+    completed: true
+}, {
+    text: 'Buy food',
+    completed: false
+}, {
+    text: 'Do work',
+    completed: false
+}, {
+    text: 'Exercise',
+    completed: false
+}]
 
-// Delete the 3rd item
-todo.splice(2, 1)
-// Add a new item onto the end
-todo.push('Email Paulo')
-// Remove the first item from the list
-todo.shift()
+// 1. Convert array to array of objects -> text, completed
 
-console.log(`You have ${todo.length} todos.`)
-// todo.forEach(function (item, index) {
-//     console.log(`${index + 1}. ${item}`)
-// })
+// 2. Create function to remove a todo by text value
+const deleteTodo = function (todos, todoText) {
+    const index = todos.findIndex(function(array) {
+        if (array.text.toLowerCase() === todoText.toLowerCase()){
+            console.log(`found!`)
+        }
+        console.log(`not found!`)
+        return array.text.toLowerCase() === todoText.toLowerCase()
+    })
+    // console.log(index)
+    if (index > -1) {
+        todos.splice(index, 1)
+        console.log(`deleted index at ${index}`)
+    }
+}
 
-for (let count = 0; count < todo.length; count ++) {
-     console.log(`${count + 1}. ${todo[count]}`)
-}
+// returns only the items that are set to completed: false
+const getThingsToDo = function (todos) {
+    return todos.filter(function (todo, index) {
+        return !todo.completed
+    })
+}
+
+console.log(getThingsToDo(todos))
+
+deleteTodo(todos, 'buy food')
+console.log(todos)