Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 56a1e67

Browse files
committed
Add a question
1 parent 9d43a1c commit 56a1e67

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

‎README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,3 +1663,58 @@ const solution = str => {
16631663
---
16641664

16651665
**[⬆ Back to Top](#javascript-coding-challenges-for-beginners)**
1666+
1667+
## 50. Check if Word Equals Summation of Two Words
1668+
1669+
Let's assume that the numeric value of a letter is its position in the alphabet starting from `0` (i.e. `a -> 0, b -> 1, c -> 2`, etc.). Similarly, the numerical value of a string `str` consisting of some lowercase English letters is the concatenation (not sum!) of the numeric values of each letter in `str`, which is then converted into an integer. For example, if `str = 'acb'`, we concatenate each letter's numeric value, resulting in `021` which is then converted to integer `21`.
1670+
1671+
You are given three strings `firstWord`, `secondWord`, and `targetWord`, each consisting of lowercase English letters `a` through `j` inclusive. Write a function that returns true if the sum of the numerical values of `firstWord` and `secondWord` equals the numerical value of `targetWord`.
1672+
1673+
```js
1674+
const isSumEqual = (firstWord, secondWord, targetWord) => {
1675+
// Your solution
1676+
};
1677+
1678+
console.log(isSumEqual('acb', 'cba', 'cdb'));
1679+
// The numerical value of firstWord 'acb' is '021' -> 21
1680+
// The numerical value of secondWord 'cba' is '210' -> 210
1681+
// The numerical value of targetWord 'cdb' is '231' -> 231
1682+
// So we return true because 21 + 210 == 231
1683+
1684+
console.log(isSumEqual('aaa', 'a', 'aab'));
1685+
// The numerical value of firstWord 'aaa' is '000' -> 0
1686+
// The numerical value of secondWord 'a' is '0' -> 0
1687+
// The numerical value of targetWord 'aab' is '001' -> 1
1688+
// So we return false because 0 + 0 != 1
1689+
1690+
console.log(isSumEqual('aaa', 'a', 'aaaa'));
1691+
// The numerical value of firstWord 'aaa' is '000' -> 0
1692+
// The numerical value of secondWord 'a' is '0' -> 0
1693+
// The numerical value of targetWord 'aaaa' is '0000' -> 0
1694+
// So we return true because 0 + 0 == 0
1695+
```
1696+
1697+
<details><summary>Solution</summary>
1698+
1699+
```js
1700+
const getNumericValue = str => {
1701+
const offset = 'a'.charCodeAt();
1702+
const arr = [];
1703+
1704+
for (let char of str) {
1705+
arr.push(char.charCodeAt() - offset);
1706+
}
1707+
1708+
return parseInt(arr.join(''));
1709+
}
1710+
1711+
const isSumEqual = (firstWord, secondWord, targetWord) => {
1712+
return getNumericValue(firstWord) + getNumericValue(secondWord) === getNumericValue(targetWord);
1713+
};
1714+
```
1715+
1716+
</details>
1717+
1718+
---
1719+
1720+
**[⬆ Back to Top](#javascript-coding-challenges-for-beginners)**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /