Created
April 10, 2018 20:53
-
-
Save Lodo4ka/b61627e6c9202a78972a41d084bb900d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { length } from './strings'; // eslint-disable-line | |
// BEGIN (write your solution here) | |
const sum = (str) => { | |
let result = 0; | |
for(let i = 0; i < length(str); i++) { | |
result += Number(str[i]); | |
} | |
return result; | |
} | |
const addDigits = (num) => { | |
let result = num; | |
while(result >= 10) { | |
result = sum(String(result)); | |
} | |
return result; | |
} | |
// END | |
export default addDigits; | |
addDigits.js | |
Реализуйте и экспортируйте по умолчанию функцию addDigits, которая работает следующим образом: | |
Дано неотрицательное целое число num. Складывать все входящие в него цифры до тех пор, пока не останется одна цифра. | |
Для числа 38 процесс будет выглядеть так: | |
3 + 8 = 11 | |
1 + 1 = 2 | |
Результат: 2 | |
Пример: | |
addDigits(10); // 1 | |
addDigits(19); // 1 | |
addDigits(38); // 2 | |
addDigits(1259); // 8 | |
Подсказка | |
Выделите процесс суммирования цифр в числе в отдельную функцию |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment