dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
2.2 KiB
2.2 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 68e39ed6106dac2f0a98fd65 | Challenge 83: Signature Validation | 28 | challenge-83 |
--description--
Given a message string, a secret key string, and a signature number, determine if the signature is valid using this encoding method:
- Letters in the message and secret key have these values:
atozhave values1to26respectively.AtoZhave values27to52respectively.
- All other characters have no value.
- Compute the signature by taking the sum of the message plus the sum of the secret key.
For example, given the message "foo" and the secret key "bar", the signature would be 57:
f (6) + o (15) + o (15) = 36
b (2) + a (1) + r (18) = 21
36 + 21 = 57
Check if the computed signature matches the provided signature.
--hints--
verify("foo", "bar", 57) should return true.
assert.isTrue(verify("foo", "bar", 57));
verify("foo", "bar", 54) should return false.
assert.isFalse(verify("foo", "bar", 54));
verify("freeCodeCamp", "Rocks", 238) should return true.
assert.isTrue(verify("freeCodeCamp", "Rocks", 238));
verify("Is this valid?", "No", 210) should return false.
assert.isFalse(verify("Is this valid?", "No", 210));
verify("Is this valid?", "Yes", 233) should return true.
assert.isTrue(verify("Is this valid?", "Yes", 233));
verify("Check out the freeCodeCamp podcast,", "in the mobile app", 514) should return true.
assert.isTrue(verify("Check out the freeCodeCamp podcast,", "in the mobile app", 514));
--seed--
--seed-contents--
function verify(message, key, signature) {
return message;
}
--solutions--
function verify(message, key, signature) {
function charValue(ch) {
if (ch >= 'a' && ch <= 'z') return ch.charCodeAt(0) - 'a'.charCodeAt(0) + 1;
if (ch >= 'A' && ch <= 'Z') return ch.charCodeAt(0) - 'A'.charCodeAt(0) + 27;
return 0;
}
function computeSum(str) {
let sum = 0;
for (let ch of str) {
sum += charValue(ch);
}
return sum;
}
const total = computeSum(message) + computeSum(key);
return total === signature;
}