As a naive approach something like this:
// Throw an error if inputKey is not correct.
// inputKey and correctKey are both strings
function checkApiKey(inputKey, correctKey) {
var isMatch = true;
for (var i = 0; i < inputKey.length; i++)
if (inputKey.charAt(i) !== correctKey.charAt(i)) {
isMatch = false;
}
}
if (!isMatch) {
throw new Error("wrong key");
}
}
So what I meant is, don't return early when isMatch = false, instead walk the entire key regardless.