#Set the count to 0 in passenger number app

1 messages · Page 1 of 1 (latest)

lone egret
#

Here's the code below. When i did it on my own, I only put count =0 because when clicking increment again count would be 0 and counel.textcontent = count = 0 so i didn't understand why the solution included adding countEl.textContent = 0 again in teh save function. any ideas?

let saveEl = document.getElementById("save-el")
let countEl = document.getElementById("count-el")
let count = 0

function increment() {
count += 1
countEl.textContent = count
}

function save() {
let countStr = count + " - "
saveEl.textContent += countStr
countEl.textContent = 0
count = 0
}

keen sierra
#

If you don't update the UI when you save (which is what the code countEl.textContent = 0 is doing), then the page won't show that the counter has been reset to 0. Instead, the UI would continue to display the previous count, even though internally the count variable has been reset to 0.

Let's say that you counted up the number 5, then tapped the save button. Internally, the counter would be reset to 0, but the UI would still show a count of 5. Then, the next time you tap the increment button, the count shown in the UI would jump from 5 to 1, which feels unnatural. That's why the UI value is also reset to 0 when the save action is processed. Does this make sense?