We provide programming data of 20 most popular languages, hope to help you!
1
3 5
7 9 11
13 15 17 19
21 23 25 27 29
rowSumOddNumbers(1); // 1
rowSumOddNumbers(2); // 3 + 5 = 8
function rowSumOddNumbers(n){
let result = [];
// generate the arrays of odd numbers
for(let i = 0; i < 30; i++){
// generate sub arrays by using another for loop
// and only pushing if the length is equal to current j
let sub = [];
for(let j = 1; j <= n; j++){
// if length === j (from 1 - n) keep pushing
if(sub[j - 1].length <= j){
// and if i is odd
if(i % 2 !== 0){
// push the i to sub (per length)
sub.push(i);
}
}
}
// push everything to the main array
result.push(sub);
}
// return sum of n
return result[n + 1].reduce(function(total, item){
return total += item;
});
}
1: 1 === 1 ^ 3
2: 3 + 5 = 8 === 2 ^ 3
3: 7 + 9 + 11 = 27 === 3 ^ 3
... etc
function rowSumOddNumbers(n) {
// how many numbers are there in the rows above n?
// sum of arithmetic sequence...
let numbers_before_n_count = (n - 1) * n / 2;
let first_number_in_nth_row = numbers_before_n_count * 2 + 1;
let last_number_in_nth_row = first_number_in_nth_row + 2 * (n - 1);
// sum of arithmetic sequence again...
return n * (first_number_in_nth_row + last_number_in_nth_row) / 2;
}
var total = numbers.reduce(function(total, current) {
return total + current;
}, 0);
console.log(total);
var array=[2,3,5,6,8,7,9]
var sum =0
for(var i=0; i<array.length; i++){
if(array[i]===0){
sum+=array[i]
console.log(sum)
}
}
///sum of odd numbers in an array javascript without loop
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
var evenNumbers = numbers.filter(function(item) {
return (item % 2 == 0);
});
console.log(evenNumbers);
var total = numbers.reduce(function(total, current) {
return total + current;
}, 0);
console.log(total);
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
var evenNumbers = numbers.filter(function(item) {
return (item % 2 == 0);
});
console.log(evenNumbers);
///sum of odd numbers in an array javascript without loop
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
var evenNumbers = numbers.filter(function(item) {
return (item % 2 == 0);
});
console.log(evenNumbers);
function findOdd(A) {
let counts = A.reduce((p, n) => (p[n] = ++p[n] || 1, p), {});
return +Object.keys(counts).find(k => counts[k] % 2) || undefined;
}
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
var evenNumbers = numbers.filter(function(item) {
return (item % 2 == 0);
});
console.log(evenNumbers);
///sum of odd numbers in an array javascript without loop
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
var evenNumbers = numbers.filter(function(item) {
return (item % 2 == 0);
});
console.log(evenNumbers);
var total = numbers.reduce(function(total, current) {
return total + current;
}, 0);
console.log(total);
Input : arr = {1, 2, 3, 4, 5, 6}
Output :Even index positions sum 9
Odd index positions sum 12
Explanation: Here, n = 6 so there will be 3 even
index positions and 3 odd index positions in an array
Even = 1 + 3 + 5 = 9
Odd = 2 + 4 + 6 = 12
Input : arr = {10, 20, 30, 40, 50, 60, 70}
Output : Even index positions sum 160
Odd index positions sum 170
Explanation: Here, n = 7 so there will be 3 odd
index positions and 4 even index positions in an array
Even = 10 + 30 + 50 + 70 = 160
Odd = 20 + 40 + 60 = 120
Even index positions sum 9
Odd index positions sum 12
Even index positions sum 9
Odd index positions sum 12
Yes
function isOdd(n) {
return Boolean(n % 2);
}
function findSum(no) {
let sum = 0;
for (var i = 0; i < no; i++) {
if (isOdd(i)) {
sum += i;
}
}
return sum;
}
console.log(findSum(100));
Copied!const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const odds = arr.filter(number => {
return number % 2 !== 0;
});
console.log(odds); // 👉️ [1, 3, 5, 7, 9]
Copied!const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const odds = [];
arr.forEach(number => {
if (number % 2 !== 0) {
odds.push(number);
}
});
console.log(odds); // 👉️ [1, 3, 5, 7, 9]