0
function getLetterData(letter) {
    letterData[letter] = { 
        count: undefined ? 1 : letterData[letter]['count']++, 
        overlap: overlapValue(letter)
    };
}

function overlapValue(letter) {
    console.log(letter); 
    return overlapLetters.includes(letter);
}

Hey all, I am trying to run a simple app to track the appearance of letters in a user-given string. I'm trying to organize data in an object that records the count for characters in the current string, and whether or not any characters have appeared in previous strings. As you can see above, to get the count I am checking if a value exists. If not, a 1 should be returned. If a value exists on count, it is incremented. My problem is I am unable to get a value out of letterData[letter]['count']; this results in a TypeError: letterData[letter] is undefined; can't access its "count" property.

I am led to believe that my letter variable is not being used on this line, and instead the program is trying to read letterData[letter]['count'] as if 'letter' is a literal string. Is there something else that must be done to access a key with a variable within a nested object? I don't have any issue getting overlapValue() to properly evaluate using the variable.

1
  • Welcome. What's the point of the ternary operator in undefined ? 1 : letterData[letter]['count']++? It will never evaluate to 1. Also, where are letterData and overlapLetters defined? Posting a full sample would be better. Commented Jan 6, 2019 at 14:01

1 Answer 1

1

undefined ? X : Y will always evaluate Y, because undefined is falsy.

I think you may want to check for whether letterData[letter] already exists and use its count (plus one) if so, or use 1 if not. To do that, you'd do this:

function getLetterData(letter) {
    letterData[letter] = { 
        count: letterData[letter] ? letterData[letter].count + 1 : 1, 
// ------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        overlap: overlapValue(letter)
    };
}

It works because if you've never seen letter before, letterData[letter] will result in undefined, but if you have seen it before it will be the previous object.

Note that that loses the previous value of letterData[letter].overlap (if that matters). It's not clear from the question whether you want the first overlap or the last, but the above will give you the last.

If you want the first, update the previous object rather than replacing it:

function getLetterData(letter) {
    const entry = letterData[letter];
    if (entry) {
        ++entry.count;
    } else {
        letterData[letter] = { 
            count: 1,
            overlap: overlapValue(letter)
        };
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you T.J., checking for letterData[letter] instead of undefined worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.