11. Sum of Odd Numbers - Java Script Fundamentals
Здравейте,
Относна задача 11 :
11.Sum of Odd Numbers
Write a program that prints the next n odd numbers (starting from 1) and on the last row prints the sum of them.
Input
You will receive a number – n. This number shows how many odd numbers you should print.
Output
Print the next n odd numbers, starting from 1, separated by newlines. On the last line, print the sum of these numbers.
Constraints
- n will be in the interval [1…100]
- Моето решние, не ми го отчита в Judge, макар че си получавам резултата съгласно заданието в Word. Може някъде да греша???
-
function OddSum(input) {
-
let n = Number(input);
-
let sum = 0;
let counter = 0;
for (let i = 1; i <=100; i +=2) {
console.log(i);
counter++;
sum += i;
if (counter == n ){
console.log (`Sum : ${sum}`);
break;
}
}
}
OddSum ( 5 )
Благодаря.