Multiplying N positive odd numbers

Viewed 439

I'm trying to get the product of N positive odd numbers

function multOdd(n) {
  var mult = 1;
  var counter=[];

  for (var i = 1; i <= 2*n-1; i += 2){
    counter.push(i);
  }

  console.log(counter);
  return mult=mult*counter[i];
}                

console.log(multOdd(10));

I pushed the numbers into an array and attempted to get the product from them but I can't get it to work.

6 Answers

If you just want the result for n, use:

function multOdd(n) {
  var result = 1;

  for (var i = 1; i <= 2*n-1; i += 2){
    result = result * i;
  }

  console.log(result);
  return result;
}                

console.log(multOdd(4));

If you want an array that has an array indexed by the number of odd numbers up to n you could use:

function multOdd(n) {
  let result = 1;
  let results = [];
  for (let i = 1; i <= 2*n-1; i += 2){
    result = result * i;
    results[(i+1) / 2] = result;
  }

  console.log(results);
  return results;
}                

console.log(multOdd(10));

When you return mult=mult*counter[i] you're only returning the multipication once. It should return mult = 1 * counter[lastElement+2] which will be wrong. In your case, the last element of counter is 19, before exiting for loop i value is i= 19 + 2 = 21. You're returning mult = 1 * 21 = 21.

You can instead return the multipication value by for loop with no need for an array:

function multOdd(n) {
  var mult = 1;

  for (var i = 1; i <= 2*n-1; i += 2){
    mult = mult * i;
  }

  return mult;
}    

There are a few ways to get the product of an array of numbers. Here are two easy ones: Relevant MDN

// Using `Array.prototype.reduce`
[3, 5, 7, 9].reduce((acc, val) => acc * val)

// Using a `for ... of` loop
let product = 1
for (const val of [3, 5, 7, 9]) {
  product *= val
}

You could separate out the two steps of your current code into two functions:

const getFirstNOddNums = (n) => {
  let oddNums = []
  for (let i = 1; i <= 2*n-1; i+=2) {
    oddNums.push(i)
  }
  return oddNums
}

const productOfArray = (arr) => {
  return arr.reduce((a, b) => a * b)
}

const N = 5
const firstNOddNums = getFirstNOddNums(N)
console.log(`The first ${N} odd numbers are: ${JSON.stringify(firstNOddNums)}`)
console.log(`The product of the first ${N} odd numbers is: ${productOfArray(firstNOddNums)}`)

let multOdd = (n) => {
    let total = 1;
    for (let i = 1; i<= 2*n; i+=2){
        total *= i;
    }
    return total;
}

console.log(multOdd(10));

Instead of recursion, We should use the standard mathematical formula for the product of first n positive odd integers which is

                                                                         the product of first n positive odd integers
Can also be written as (in the form of pi notation)

                                                                         enter image description here

For this latex image, I used https://codecogs.com/latex/eqneditor.php.

Factorial is

n! = n(n-1)(n-2)(n-3) ... and so on

So we can use Array(n).fill() to get an array of 10 elements and reduce them to get a factorial by

Array(n).fill().reduce((v,_,i) => (i+1) * v || 2)

Then we divide it by 2 to the power n times the n!. Which is what we want. The advantage here is that, this makes your solution, a one liner

let n = 10
let answer = Array(2*n).fill().reduce((v,_,i) => (i+1) * v || 2) / (Math.pow(2,n) * Array(n).fill().reduce((v,_,i) => (i+1) * v || 2))
console.log(answer)

Related