Bug on the challenge?

Tell us what’s happening:
I suspect there might be a bug on the platform because my code fails on

The sum function should use the ... rest parameter on the args parameter.

but my code clearly includes the ...rest parameter. I can back my code with Mozilla’s Javascript interpreter which outputted desired result.

Your code so far


function sum(...args) {
if (args.length == 0) {
  return 0;
} else {
return args.reduce((previous, current) => {
  return previous + current;
});
}
}
console.log(sum(1, 2, 3)); // 6

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:71.0) Gecko/20100101 Firefox/71.0.

Challenge: Use the Rest Parameter with Function Parameters

Link to the challenge:

1 Like

There was no need to change the reduce function or to add extra logic in the function. The changes to the body of sum is confusing the test suite.

1 Like

Thank you throwing some light on this.
I edited my code to make it look like this:

const sum = (function() {
  "use strict";
  return function sum(...args) {
    return args.reduce((previous,current) => previous+current, 0);
  };
})();
console.log(sum(1, 2, 3)); // 6

Which brought me closer to what I’m supposed to have but guess what, I’m still getting the error about “not using …rest parameter” I’m scratching my head here. What else the test suite wants from me ?

You’re still doing extra work. You don’t need to wrap sum() inside another function named sum.

ah, right… the hint should be changed so it reflects the not necessity of declaring the strict mode

I will do it (if someone does not do that before me) as soon as I access a computer

This is what I’ve got:

function sum(...args) {
    return args.reduce((previous,current) => previous+current, 0);
  };

It passes all the tests except from the “…rest” one. Still not recognizing the fact that I’m using the parameter in the brackets. Unless it’s checking also for something else.

Reset the challenge and do not change the reduce() function at all.

OK. This is funny. All I had to do at the beginning is to edit a,b,c to ...args and I was editing the whole damn thing.

Look at how much you learned though :smiley:

Almost broke my laptop in half in process but yeah, nice learning experience. Have a good one !