0

I'm creating a small program where I have a page with two input fields and a user can enter their name and the number of calories they've eaten today. After a person enters their data, I want to display it alongside other people's data in a list sorted by total calories.

I also want that person to be able to re-enter their name and additional calories and have it update their total calories (as opposed to creating another entry with their name and most recent calorie amount).

I am assigning the input values to variables, using those to create a new Person object, then pushing that object to an array.

How can I test to see if the array contains a Person object with a name that already exists? My test isn't recognizing names that have already been submitted. If a person's name has already been entered, I'd like to update their total calories instead of creating a new Person object.

My javascript code:

(function () {
    "use strict";

    /* Person object constructor */
    function Person(name, calories) { 
        this.name = name;
        this.calories = calories;
    }

    function addToList() {
        var name = document.getElementById('name').value;
        var calories = document.getElementById('calories').value;

        /* 
            Check to see if list already contains person's name
            If yes, update their calorie amount.
            If not, create a new player and add them to the list.
         */

        for (var i = 0; i < list.length; i++) {
            if (list[i] === name) {
                alert('This person already exists.');
            } else {
                var newPerson = new Person(name, calories);
                list.push(newPerson);
            }   
        }
    }

    var list = [];
    $('#add').click(addToList);

})();
3
  • Is the question "how do I check if the person already exists in the list", or "how do I modify the person if they already exist in the list"? I ask because, your code already checks if the person exists in the list (unless I'm misreading). Commented Sep 30, 2013 at 18:46
  • 1
    If you plan on building on your example I suggest try giving the Person an ID property or email.. because you can have multiple people with the same names. Commented Sep 30, 2013 at 19:01
  • Pricey, that's a great idea; I'll look into that as well, thank you. Commented Sep 30, 2013 at 19:21

2 Answers 2

2

Your list is a list of Person (if rankings.push is meant to be list.push) so when you are doing (list[i] === name) that is trying to compare a Person object to a string literal. Try doing (list[i].name.toLowerCase() === name.toLowerCase())

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like you're storing names inside of the array list, however these names are never being ADDED to list. Instead you appear to be using another array called rankings.

rankings.push(newPerson);
list.push(name);

And since you already have an array that stores people, rankings, maybe you should iterate over that?

var foundPerson = false;
for (var i = 0; i < rankings.length; i++) {
    if (rankings[i].name === name) {
        alert('This person already exists.');

        // do your update
        rankings[i].calories = calories;

        // set flag so we know we actually found a person
        foundPerson = true;
        break;
    } 
}

// if no person found, add new person
if (!foundPerson) {
    var newPlayer = new Person(name, calories);
    rankings.push(newPerson);
}

1 Comment

Thanks Bucky, I did have two typos I created when I transcribed and simplified my code to post here, now edited. I'll try out your suggestion.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.