Addition question for JavaScript fractions. I don't know what the problem is.

Asked 1 years ago, Updated 1 years ago, 320 views

The test is correct, but I just submit it and it's wrong...

function solution(numer1, denom1, numer2, denom2) {
    var answer = [];
    if(denom1 === denom2) {
        answer.push(numer1 + numer2)
        answer.push(denom1 + denom2)
    } } else if(denom1 < denom2) {
        if((denom2 % denom1) === 0){
            let gcf = Math.trunc(denom2 / denom1)
            let denom1_res = denom1 * gcf
            let numer1_res = numer1 * gcf
            answer.push(numer1_res+numer2)
            answer.push(denom2)
        } } else if((denom2 % denom1) !== 0) {
            let gcf = Math.trunc(denom2 / denom1) 
            let lcm = (denom1*denom2) / gcf
            answer.push(numer1*Math.trunc(lcm / denom1) + numer2*Math.trunc(lcm / denom2))
            answer.push(lcm)
        }
    } } else if(denom2 < denom1) {
        if((denom1 % denom2) === 0){
            let gcf = Math.trunc(denom1 / denom2)
            let denom2_res = denom2 * gcf
            let numer2_res = numer2 * gcf
            answer.push(numer2_res+numer1)
            answer.push(denom1)
        } } else if((denom1 % denom2) !== 0){
            let gcf = Math.trunc(denom1 / denom2)
            let lcm = (denom2*denom1) / gcf
            answer.push(numer1*Math.trunc(lcm / denom1) + numer2*Math.trunc(lcm / denom2))
            answer.push(lcm)
        }
    }
    return answer;
}

javascript

2023-01-30 11:24

1 Answers

1/2 + 1/2 = 1.

The questioner's code is two-quarters.


2023-01-30 11:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.