Pārlūkot izejas kodu

Complete array methods

Signed-off-by: Michael Tang <michael.h.tang@gmail.com>
Michael Tang 5 gadi atpakaļ
vecāks
revīzija
9673116af8
2 mainītis faili ar 20 papildinājumiem un 4 dzēšanām
  1. 12 1
      notes.js
  2. 8 3
      todo.js

+ 12 - 1
notes.js

@@ -1,4 +1,15 @@
 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'
+
 console.log(notes.length)
-console.log(notes[notes.length - 2])
+console.log(notes)
+

+ 8 - 3
todo.js

@@ -1,6 +1,11 @@
 const todo = ['Call with Jon', "Return Mom's trimmer", 'Get Insurance', 'Register for wcb', 'Wake up for christmas']
 
-console.log(`You have ${todo.length} todos.`)
+// 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()
 
-console.log(todo[0])
-console.log(todo[todo.length - 1])
+console.log(`You have ${todo.length} todos.`)
+console.log(todo)