VictoriaTodorova
VictoriaTodorova

Reputation: 11

I cannot solve a math problem related to scores and bonus points

The math problem I am struggling to solve says:

There is a figure which is the starting point. We need to create a program which calculates the bonus points added to the score + the total points which are the score + the bonus.

If the starting point (the score) is less than or equals to 100 - the bonus points are 5. If the score is bigger than 100, the bonus is 20% of the score. If score is bigger than 1000, the bonus is 10% of the score.

Additional bonus points (they have to be added separately from the previous ones): if the score is an even number -> +1 point if the score ends with 5 -> + 2 points

function solve(input) {
    let score = Number(input.shift());
    let bonus = 0.00;

    if (score <= 100) {
        bonus = 5;
    } else if (score > 100) {
        bonus = 0.20 * score
    } else if (score > 1000) {
        bonus = 0.10 * score
    } else 

    if (score % 2 === 0) {
        bonus = bonus + 1
    } else if (score % 10 === 5) {
        bonus = bonus + 2
    } else

    console.log(bonus)
    console.log(score + bonus)

}

When I put 20 as an argument, I expect a result of 6 being the bonus and 26 being the total points.

Upvotes: 0

Views: 87

Answers (1)

trincot
trincot

Reputation: 350310

The main issue is that you are trying to have empty else clauses, but what really happens is that the statement that follows it becomes part of that else block.

This is the consequence of your choice to not end statements with semi-colons (always dangerous!). If you would have placed a semi-colon after the final else there would not be this problem.

Secondly, your sequence of if conditions will mean that the bonus will never get to be 0.20 * score, because scores greater than 1000 will end up in the case score > 100.

In fact, that condition score > 100 will always be true once the code execution gets there, since at that moment is certain that score <= 100 is not true.

So here is a correction:

function solve(input) {
    let score = Number(input.shift());
    let bonus;

    if (score <= 100) {
        bonus = 5;
    } else if (score < 1000) {
        bonus = 0.20 * score;
    } else { // certain that score >= 1000
        bonus = 0.10 * score;
    }

    if (score % 2 === 0) {
        bonus = bonus + 1;
    } else if (score % 10 === 5) {
        bonus = bonus + 2;
    }

    return score + bonus;
}

console.log(solve([101]));

Note that you can make this more compact by using the ternary operator. Also score % 2 === 0 is exactly the bonus that should be added (true becomes 1 when put in an arithmetic expression) for odd numbers.

This decreases readability somewhat, so maybe you prefer not to do this:

function solve(input) {
    let score = Number(input.shift());

    return score 
        + ( score <= 100 ? 5
          : (score < 1000 ? 0.20 : 0.10) * score )
        + (score % 2 === 0) 
        + (score % 10 === 5) * 2;
}

console.log(solve([101]));

Upvotes: 1

Related Questions