最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make a function that returns the factorial of each integer in an array - Stack Overflow

programmeradmin3浏览0评论

I'm new to coding and have been given this question that I cannot seem to get right:

Create a function that takes an array of positive integers and returns an array of the factorials of these numbers.

  E.g. [4, 3, 2] => [24, 6, 2]

The factorial of a number is the product of that number and all the integers below it.

  E.g. the factorial of 4 is 4 * 3 * 2 * 1 = 24

If the number is less than 0, reject it.

The code that I have created is this;

function getFactorials(nums) {
   if (nums === 0 || nums === 1)
      return 1;
   for (var i = nums - 1; i >= 1; i--) {
      nums *= i;
   }
   return nums;
} 

The code is being run against this test;

describe("getFactorials", () => {
  it("returns [] when passed []", () => {
    expect(getFactorials([])).to.eql([]);
  });
  it("returns one factorial", () => {
    expect(getFactorials([3])).to.eql([6]);
  });
  it("returns multiple factorials", () => {
    expect(getFactorials([3, 4, 5])).to.eql([6, 24, 120]);
  });
  it("returns largest factorials", () => {
    expect(getFactorials([3, 8, 9, 10])).to.eql([6, 40320, 362880, 3628800]);
  });
});

How should I do this?

I'm new to coding and have been given this question that I cannot seem to get right:

Create a function that takes an array of positive integers and returns an array of the factorials of these numbers.

  E.g. [4, 3, 2] => [24, 6, 2]

The factorial of a number is the product of that number and all the integers below it.

  E.g. the factorial of 4 is 4 * 3 * 2 * 1 = 24

If the number is less than 0, reject it.

The code that I have created is this;

function getFactorials(nums) {
   if (nums === 0 || nums === 1)
      return 1;
   for (var i = nums - 1; i >= 1; i--) {
      nums *= i;
   }
   return nums;
} 

The code is being run against this test;

describe("getFactorials", () => {
  it("returns [] when passed []", () => {
    expect(getFactorials([])).to.eql([]);
  });
  it("returns one factorial", () => {
    expect(getFactorials([3])).to.eql([6]);
  });
  it("returns multiple factorials", () => {
    expect(getFactorials([3, 4, 5])).to.eql([6, 24, 120]);
  });
  it("returns largest factorials", () => {
    expect(getFactorials([3, 8, 9, 10])).to.eql([6, 40320, 362880, 3628800]);
  });
});

How should I do this?

Share Improve this question edited Oct 12, 2022 at 16:25 General Grievance 5,03338 gold badges37 silver badges56 bronze badges asked Dec 22, 2018 at 20:09 GeoGeo 4472 gold badges6 silver badges18 bronze badges 1
  • 2 You should create a factorial function that only deals with one number, then, just map it over the list. Tying the handling of multiple numbers and the putation of each factorial will just confuse matters. – Carcigenicate Commented Dec 22, 2018 at 20:12
Add a ment  | 

4 Answers 4

Reset to default 5

First off, make a recursive function that calculates the factorial of a single number:

function factorial(num) {
    if (num == 0 || num == 1) {
        return 1;
    }
    return num * factorial(num - 1);
}

Then to do it for an array, just use Array.prototype.map() like so:

function getFactorials(arr) {
    var result = arr.map(x => factorial(x));
    return result;
}

Here's a demonstration:

 

function factorial(num) {
    if (num == 0 || num == 1) {
        return 1;
    }
    return num * factorial(num - 1);
}

function getFactorials(arr) {
    var result = arr.map(x => factorial(x));
    return result;
}

console.log(getFactorials([4, 8, 10])); 
console.log(getFactorials([]));
console.log(getFactorials([1, 2, 3, 4, 5]));

Hopefully this helps!

You need to separate function into two functions, one for iterating the array and collecting the calculated values and the other to get the facorial of a number.

function getFactorials(nums) {
    var result = [],
        i;
        
    for (i = 0; i < nums.length; i++) {
        result.push(getFactorial(nums[i]));
    }
    return result;
} 

function getFactorial(n) {
   if (n === 0 || n === 1) return 1;
   return n * getFactorial(n - 1);
}

console.log(getFactorials([]));
console.log(getFactorials([3]));
console.log(getFactorials([3, 4, 5]));
console.log(getFactorials([3, 8, 9, 10]));

This method will take an array of numbers and will return an array of factorial numbers of them,

function getFactorials(array) {
        var facArray = [];
        for (var j = 0; j < array.length; j++) {
            num = array[j];
            if (num === 0 || num === 1)
                return 1;
            for (var i = num - 1; i >= 1; i--) {
                num *= i;
            }
            facArray.push(num);
        }
        return facArray;

    }

console.log(getFactorials([4, 3, 2]));
function getFactorials(nums) {
  return numbers = Array.from(nums).map(function factorializeSingleNumber(num) {
    if (num == 0 || num == 1) return 1
    else return num * factorializeSingleNumber(num - 1)
  })
}

The best way I find of tackling these is to break the problem down. So in this case, work a solution that factorializes a single number. When that is working you can add this 'working code' into a map or loop, to work through the array.

My above solution uses recursion but could be a simple 'for loop' too

发布评论

评论列表(0)

  1. 暂无评论