diff --git a/index.html b/index.html index 8046f00..4d00af8 100644 --- a/index.html +++ b/index.html @@ -21,6 +21,18 @@

Note Taker App

var textElement = document.getElementById("note") function saveNote() { - + if (textElement.value === ""){ + return alert("text is empty"); + } + + const id = getNoteId(); + let noteObject = getExistingNotes(); + if(!noteObject){ + noteObject = {} + } + + noteObject[id] = textElement.value; + localStorage.setItem('notes', JSON.stringify(noteObject)); + alert("Note Saved!") } diff --git a/index.js b/index.js index e69de29..5afd5af 100644 --- a/index.js +++ b/index.js @@ -0,0 +1,19 @@ +function getNoteId() { + let noteObject = getExistingNotes() + if(!noteObject){ + return 1 + } + + const keysArray = Object.keys(noteObject); + const numberKeys = keysArray.map((key) => Number(key)) + console.log(numberKeys); + return Math.max( ... numberKeys) + 1; +} + +function getExistingNotes() { + let notes = localStorage.getItem('notes') + if(!notes){ + return null + } + return JSON.parse(notes); +} diff --git a/index2.html b/index2.html index b4394a5..16c3820 100644 --- a/index2.html +++ b/index2.html @@ -15,6 +15,17 @@ \ No newline at end of file + ( + function getNotes(){ + let notes = getExistingNotes(); + let notesArray = Object.values(notes); + let orderedList = document.getElementById('notes-list') + notesArray.forEach(note => { + let listItem = document.createElement('li') + listItem.innerHTML = note + orderedList.appendChild(listItem) + + }) + })() + +