throv
throv

Reputation: 11

Different factorial of a number using javascript

I'm trying to come up with a function that gives me a "factorial" of a number but in a different way. For example:

15! = 15*14/13+12-11*10/9... and so on.

I tried doing this by first creating an array and, then, a loop to get to the result. But it doesn't work as expected. The result expected from the code below is 13.

function createList(n) {
    let input = n
    let list = [input]

    for(i = input-1; i >= 1; i--) {
        list.push(i)
    }
    return list
}



function factorialDiff() {
    let elements = createList(12)
    var res = elements.shift()
    while(elements.length >= 1) {
    if(elements.length == 0) {
        return res
    }
    res *= elements.shift()
    if(elements.length == 0) {
        return res
    }
    res /= elements.shift()
    if(elements.length == 0) {
        return res
    }
    res += elements.shift()
    if(elements.length == 0) {
        return res
    }
    res -= elements.shift()
    if(elementos.length == 0) {
        return res
    }
    }
    return res 
}
console.log(factorialDiff())
``



`

Upvotes: 1

Views: 61

Answers (2)

Faezeh Keshmiri
Faezeh Keshmiri

Reputation: 137

Is this what you looking for?

function myFactorial(num) {
        var result = num;
        operatorNum = 0;
        for(i = num - 1; i >= 1; i--){
            if(operatorNum % 4 == 0){ // "*"
                result = result * i;
            }else if (operatorNum % 4 == 1){  // "/"
                result = result / i;
            }else if (operatorNum % 4 == 2){ // "+"
                result = result + i;
            }else{ // "-"
                result = result - i;
            }
            operatorNum++;
        }
        return result;
    }
    console.log(myFactorial(4)); // logs 7

Upvotes: 1

André
André

Reputation: 2016

This is what your code is doing:
console.log(((((12*11)/10+9-8)*7/6+5-4)*3)/2+1);

In math the convention is to solve multiplication/division first and plus/minus second in this order. However, the code you did is solving the operations from left to right.

For example:

console.log(12*11/10+9-8*7/6+5-4*3/2+1); // returns 12.86

console.log(((((12*11)/10+9-8)*7/6+5-4)*3)/2+1); // returns 27.35 just like your function does

If your expected behavior is the first line, the following function should solve your problem:

function factorialDiff() {
    const elements = createList(12);
    let res1 = elements.shift();
    let res2 = elements.shift();
    let res3 = elements.shift();
    let res = (res1*res2/res3)
    while(elements.length >= 1) {
        res += elements.shift();
        if(elements.length == 0) {
            return res;
        }
        res1 = elements.shift();
        if(elements.length == 0) {
            return res*res1;
        }
        res2 = elements.shift();
        if(elements.length == 0) {
            return res*res1/res2;
        }
        res3 = elements.shift();
        res -= (res1*res2/res3);
        if(elements.length == 0) {
            return res;
        }        
    }
    return res;
}

Upvotes: 2

Related Questions