Problem overview
The method safeLookup receives scores, an object from names to scores, and a name. It returns the name's score, or 0 when name isn't a key of scores.
With scores = {"Ana": 90, "Ben": 75, "Cara": 88}, the name "Ben" gives 75 and "Dan" gives 0. An empty object has no keys, so every name gives 0. Spelling counts: "ana" is not the key "Ana", so it also gives 0.
Approach: check first, then read
Reading a missing key from an object doesn't fail. It gives undefined. With the scores above, scores["Dan"] is undefined, and returning that would break the lesson, which wants 0.
The fix is to ask whether the key exists before reading it. The in operator does that: name in scores is true when scores has a key spelled like the value of name, and false otherwise. If the answer is yes, return the stored score. Otherwise fall through to a default of 0.
Two other idioms give a default as well: scores[name] ?? 0 uses the right side when the left side is undefined or null, and Object.hasOwn(scores, name) asks the same yes or no question as in. The lesson uses the explicit if with in so each decision is visible in the code.
Step by step
- Write the check. An
ifwith the conditionname in scores. Inside it, returnscores[name]. Brackets are needed becausenameis a variable. - Return the default. After the
if's closing brace, return0. Code only gets there when the key is missing, because areturninside theifhas already ended the method otherwise.
The solution in JavaScript
class Solution {
safeLookup(scores, name) {
if (name in scores) {
return scores[name];
}
return 0;
}
}
Details worth knowing:
- The order of operands in
in. The key goes on the left and the object on the right.name in scoresasks about keys. It doesn't search the values. - No
elseis needed. Areturnleaves the method immediately, so the finalreturn 0only runs when theifdidn't. - Inherited keys. The
inoperator also reports properties an object inherits. For an empty object{},"toString" in {}istrue, sosafeLookup({}, "toString")returns the functiontoStringinstead of0. The lesson's names are plain words of letters that never collide with such built-in names in practice, but remember it when keys come from users.Object.hasOwn(scores, name)checks only the object's own keys. - A score of
0. A key can exist with the value0. Because the solution checks for the key and not for a truthy value,{"Ana": 0}returns its stored0, which happens to look the same as the default.
Edge cases and common mistakes
- An empty object.
{}with"Ana"returns0, because"Ana" in {}isfalse. - Different case.
"ana"against the key"Ana"gives0: keys are compared exactly. - Skipping the check.
return scores[name]alone works for names that exist, but returnsundefinedfor the others. - Testing the value instead of the key.
if (scores[name])treats a stored score of0as missing. Here it still returns the right number, but the check says the wrong thing, and it breaks for any default other than0. - Returning inside the
ifonly. Without the finalreturn 0, the method returnsundefinedfor missing names.
Testing
{"Ana": 90, "Ben": 75, "Cara": 88}with"Ben"gives75.- The same object with
"Dan"gives0. {}with"Ana"gives0.- The same three-name object with
"ana"gives0.
