-
-
Save alexey-kar/527d8d7f001456e4e405362b3a3136ff to your computer and use it in GitHub Desktop.
Нужно написать функцию которая будет возвращать сумму чисел из строки n, куда входят только нечетные числа
This file contains hidden or 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
{ | |
class Test { | |
static assertEquals(test_value, should_value) { | |
return test_value === should_value; | |
} | |
} | |
const string_pyramid = ` | |
1 | |
3 5 | |
7 9 11 | |
13 15 17 19 | |
21 23 25 27 29 | |
`; | |
/** | |
* Суммирует все нечётные числа в пирамиде которые находятся в указанном номере строки | |
* | |
* @param {string} pyramid_text - текст с пирамидой чисел | |
* @param {string} line_number - номер строки в пирамиде, в человеческом представлении 1, 2, 3 | |
* @return number | |
*/ | |
const magic_function = (pyramid_text, line_number) => { | |
const lines = pyramid_text.split("\n").filter((l) => l.length); | |
const numbers = lines[line_number - 1] == undefined ? [] : lines[line_number - 1].trim().split("\t\t").filter(number => Number(number) % 2 === 1); | |
let sum = 0; | |
numbers.map(n => sum += Number(n)); | |
return sum; | |
}; | |
console.log( Test.assertEquals(magic_function(string_pyramid, 1), 1) ); | |
console.log( Test.assertEquals(magic_function(string_pyramid, 2), 8) ); | |
console.log( Test.assertEquals(magic_function(string_pyramid, 42), 74088) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment