Spotting Remote Postcodes With String Keys

Marta’s next label goes to Sam Tait in Lerwick, postcode ZE1 0AA. Her courier charges £4.50 extra for Shetland and the Outer Hebrides. You’ll port the remote-area check so order 1043 includes that surcharge.

Check the Pair’s Identity

Python stores the remote areas as REMOTE_AREAS = {("GB", "ZE"), ("GB", "HS")}. Checking ("GB", "ZE") in REMOTE_AREAS gives True, and adding that tuple again leaves two members. A tuple of strings compares and hashes by content.

JavaScript has no tuple type, so short arrays can hold the pairs. Try that port with the Set’s .has() and .add() calls. Predict the membership result and size:

Language: JavaScript
const REMOTE_AREAS = new Set([["GB", "ZE"], ["GB", "HS"]]);
const country = "GB";
const area = "ZE1 0AA".slice(0, 2);
console.log(REMOTE_AREAS.has([country, area]));
REMOTE_AREAS.add([country, area]);
console.log(REMOTE_AREAS.size);

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.