mercredi 2 octobre 2019

else statement in javascript

I'm very new to programming so please excuse this basic question. This code wont run as the last "else" statement near the bottom of the code which executes the code {console.log("invalid"} returns an error in the console in google chrome and I don't know how to fix it. When I execute the same app in ruby and I run it in the ruby terminal it works fine but for some reason in javascript it doesn't work. Can someone please help me?

var contacts = [
    {
    Name: "Scott McMahon",
    Contact: "04158656",
    Email: "scotttt@y7mail.com",
    Address: "3 rrhfh, vic 30555",
    }
];

var user_interface = () => {
    console.log("XXXXXXXXXXXXXXXXXXXXXXXXXX")
    console.log("These are your options")
    console.log("XXXXXXXXXXXXXXXXXXXXXXXXXX")
    console.log("1: Add a contact")
    console.log("2: Remove a contact")
    console.log("3: View contacts")
    console.log("4: EXIT")
    console.log("XXXXXXXXXXXXXXXXXXXXXXXXXX")
    let input = prompt("What would you like to do?")
    return input;
};

var user_input = user_interface()

while (user_input != 4 && user_input.toUpperCase() != "EXIT") {

    if (user_input == 1 || user_input.toUpperCase() == "ADD A CONTACT") {
        let addName = prompt("Please enter the name of the person you would like to add")
        let addNumber = prompt("What is the number you would like to add?")
        let addEmail = prompt("What is the persons email address?")
        let addAddress = prompt("What is this persons address?")

        let person_to_add = {
            Name: addName,
            Contact: addNumber,
            Email: addEmail,
            Address: addAddress,
        }

        contacts.push(person_to_add)

    } else if (user_input == 2 || user_input.toUpperCase() == "REMOVE A CONTACT") {

        for (person of contacts) {
            console.log(person.Name)
        } 

        let name = prompt("Which contact would you like to remove?")

        let index = 0
        for (person of contacts) {
            if (person.Name.toUpperCase() == name.toUpperCase()) {
            contacts.splice(index, 1)
            }
            index += 1
            } else {
               console.log("invalid")
                    }

    } else if (user_input == 3 || user_input.toUpperCase() == "VIEW CONTACTS") {
        for (person of contacts) {
        console.log(person.Name)
        }
    }
    var user_input = user_interface()
};

Aucun commentaire:

Enregistrer un commentaire