I’m just learning Javascript. I’m trying to create a basic application, a form asks for a first and last name. I want to check whether that name exists in an array, if it exists an error is given as an alert. if the name doesn’t exist, the name is added to the array and displayed to output. I’ve added the code to this, but for some reason the program is still allowing duplicate names. Is there anyone who can help with this? Also, my clear list is only clearing the last element rather than the entire array. Anyone know this one too?
let guest_list = []
function getGuestList() {
if (localStorage.getItem('names') && localStorage.getItem("names") != '[ ]') {
return JSON.parse(localStorage.getItem('names'))
} else {
return guest_list
}
}
function addNewGuest(event) {
event.preventDefault()
let f = document.querySelector("#first_name").value
let l = document.querySelector("#last_name").value
let names = getGuestList()
if (f === '' || l === '') {
alert('First and last name required')
}
if (f && l) {
let name = { fName: f, lName: l}
if (guest_list.includes(name)) {
alert('Name already exists')
} else {
names.push(name)
localStorage.setItem('names', JSON.stringify(names))
}
}
displayGuestList()
document.querySelector("#first_name").value = ''
document.querySelector("#last_name").value = ''
}
function clearForm(event) {
let names = getGuestList()
for (let n of names) {
localStorage.clear(n)
}
displayGuestList()
}
document.querySelector("#myForm").onsubmit = addNewGuest
document.querySelector("#reset").onclick = resetForm
document.querySelector("#clear").onclick = clearList
displayGuestList()
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
Can you provide a full working example (with the HTML).
If you use includes on an array of objects, it will do a reference comparison. If the two objects are the same it will be true, if they are not the same objects but contain the same key/values it will be false.
let guest_list = [
]
function getGuestList() {
if (localStorage.getItem('names') && localStorage.getItem("names") != '[]') {
return JSON.parse(localStorage.getItem('names'))
} else {
return guest_list
}
}
//add name to the guest_list array. First checks whether the first name or the last name is empty. If it is, an alert pops.
//Then, if first name and last name hold values, create a name variable to hold the first name and the last name. the name passes to the checkName() function, if function returns true, an alert pops, if it returns false the name is added to the array.
function addNewGuest(event) {
event.preventDefault()
let f = document.querySelector("#first_name").value
let l = document.querySelector("#last_name").value
let names = getGuestList()
if (f === '' || l === '') {
alert('First and last name required')
}
if (f && l) {
let name = { fName: f, lName: l}
if (guest_list.includes(name)) {
alert('Name already exists')
} else {
names.push(name)
localStorage.setItem('names', JSON.stringify(names))
}
}
//if (f && l) {
// let name = { fName: f, lName: l }
// names.push(name)
// localStorage.setItem('names', JSON.stringify(names))
//}
displayGuestList()
document.querySelector("#first_name").value = ''
document.querySelector("#last_name").value = ''
}
function resetForm(event) {
document.querySelector("#first_name").value = ''
document.querySelector("#last_name").value = ''
}
function displayGuestList() {
let names = getGuestList()
let guest_html = ''
let ndx = 0
for (let n of names) {
guest_html += `
<tr>
<td>${n.fName}</td>
<td>${n.lName}</td>
</tr>
`
ndx++
}
document.querySelector("#guestList").innerHTML = guest_html
}
function clearList(event) {
let names = getGuestList()
for (let n of names) {
localStorage.clear(n)
}
displayGuestList()
}
document.querySelector("#myForm").onsubmit = addNewGuest
document.querySelector("#reset").onclick = resetForm
document.querySelector("#clear").onclick = clearList
displayGuestList()
Did you understand what I was saying in the above post?
Object compare doesn’t work like that. You have to check the values inside the objects or you can use JSON.stringify() although that is not always advisable.