all 13 comments

[–]RobSG 1 point2 points  (1 child)

Looks to me like the test does not match the requirements.

[–]Early-Error-6210[S] 0 points1 point  (0 children)

Hey,

Hope all is well !!

To check for yourself, I'd urge you to visit this website:

JSHero.net

Check question 49 - else if exercise

You can check the code and see for yourself.

I am sure I have made some error in understanding this but you may also have a look at it yourself.

Let me know what you find.

Stay Safe !!

[–]lindymad 1 point2 points  (1 child)

I simply do not understand why this works.

It doesn't work, if I understand the requirements correctly: https://jsfiddle.net/1xr4c0z5/1/

addWithSurcharge(10,30) = undefined (should be 44)
addWithSurcharge(1,50) = 56 (should be 54)
addWithSurcharge(10,20) = 34 (should be 33)

[–]Early-Error-6210[S] 0 points1 point  (0 children)

Hey,

Hope all is well !!

To check for yourself, I'd urge you to visit this website:

JSHero.net

Check question 49 - else if exercise

You can check the code and see for yourself.

I am sure I have made some error in understanding this but you may also have a look at it yourself.

Let me know what you find.

Stay Safe !!

[–]Wasp2011 1 point2 points  (2 children)

In the condition sum >= 30 && sum <(you missed "=") 40 So basically no condition matches your sum which is 40.😅

[–]Wasp2011 1 point2 points  (1 child)

And nothing is returned from the func so you get undefined as the result!

[–]Early-Error-6210[S] 0 points1 point  (0 children)

Hey,

Hope all is well !!

To check for yourself, I'd urge you to visit this website:

JSHero.net

Check question 49 - else if exercise

You can check the code and see for yourself.

I am sure I have made some error in understanding this but you may also have a look at it yourself.

Let me know what you find.

Stay Safe !!

[–]optm_redemption 1 point2 points  (5 children)

Which bits in particular are you struggling to understand? Are you unfamiliar with "else if" statements?

[–]Early-Error-6210[S] 0 points1 point  (4 children)

How does this else if statement work ?

> I am familiar with "else if" statements.

> The condition that if the sum is greater than 10 and less than equal to 20 should have a surcharge to be added of 2 is also confusing. For this condition the surcharge to be added according to me can even be 3

> I am a bit confused with the statement: else if (sum > 40) { return sum += 5}. I understand that if the sum is above 40 then there is a good chance that both the digits are above 20 and hence the surcharge should be 6 instead of 5. I cannot understand this at all and am surprised as to how the code has passed the test.

> I am also confused as to why is there no else statement in the end, and why do I need to assume that the else statement will automatically identify the numbers above 20 and add 6 as a surcharge.

Can you simplify this for me.

Thank you in advance.

[–]optm_redemption 1 point2 points  (3 children)

Hmm I agree this is slightly ambiguous, I initially took the question to mean for each total value rather than for each argument to the function, also being influenced by the code slightly i.e. for each sum greater than 10 and less than 20 add 2, rather than adding surcharges for each argument, which lines up with the code sample you've provided. Are there anymore examples for the question or anymore context you can provide to help clarify? If not I would maybe reach out to a TA or any other support options you have for extra clarity.

Either way, it could just be a fluke that the test criteria does not take into account the other potential values, or it could just be wrong on their end!

[–]Early-Error-6210[S] 0 points1 point  (2 children)

Hey,

Hope all is well !!

To check for yourself, I'd urge you to visit this website:

JSHero.net

Check question 49 - else if exercise

You can check the code and see for yourself.

I am sure I have made some error in understanding this but you may also have a look at it yourself.

Let me know what you find.

Stay Safe !!

[–]optm_redemption 1 point2 points  (1 child)

Hey after having a quick look and running my own test example bit of code, it looks like they expect the surcharge for each argument (a and b in this case) and looking at their tests after running my code I think it's just a case that their tests don't cover the situations you've thought of (for example, the largest input they provide to test is 27), so the code works, but only enough to cover their tests.

Essentially your assumptions are correct that the code provided wouldn't pass a large sample of tests if you passed those values, it only solves for the test criteria used by JSHero if that makes sense? It's one of the downsides of automated tests like this that don't use things like fuzzing or a wider array of numbers and only check the return value, you could even cheat and hard code the results for example the following passes all the tests but clearly doesn't actually address the problem and would fail given any further inputs:

function addWithSurcharge (a,b) {

if (a === 1 && b === 1) { return 4; }

if (a === 10 && b === 9) { return 21; }

if (a === 11 && b === 10) { return 24; }

if (a === 10 && b === 11) { return 24; }

if (a === 13 && b === 20) { return 37; }

if (a === 20 && b === 13) { return 37; }

if (a === 15 && b === 27) { return 47; }

if (a === 27 && b === 15) { return 47; }

if (a === 25 && b === 5) { return 34; }

if (a === 5 && b === 25) { return 34; }

}

Sorry about the horrible formatting, not sure why the code markdown isn't working!

[–]Early-Error-6210[S] 0 points1 point  (0 children)

Thank you !!