How to add 1 to the number using the "for" command when the number N is a function parameter?

Asked

Viewed 220 times

4

Being n the value of 5, adding up 1 + 2 + 3 + 4 + 5, resulting in the return.

I tried to do it like this, but it didn’t work:

function (n){

for ( var i = 0; i < n.length; i++ ){
   total += n[i];
}

return total;
  • Your question is not clear. The parameter n is it a number (as the question text says) or an array? Why are you doing n[i] - indicating that n would be an array (or object)?

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

1 answer

7

I imagine you got some code ready that sweeps through a collection of data that sums up your values. Learning based on ready-made code is very popular, but it’s also the reason why people don’t understand what’s going on there, and programming is just understanding what’s going on.

So what you don’t want is to go through a collection, is to go through a numerical sequence, you don’t have to have a array how it was used. You just have to count without access to array some. And of course, you must accumulate the value in each step, as you did.

Nor would it be possible to catch the length of nbecause n it does not have a size, it is only a very simple number, who has size are data collections.

There are other errors. One of them is that it did not initialize the accumulator variable. It even works, but it is gambiarra.

Another is that the function has no name.

And finally can not start from 0, so described should start from 1 and go to the last number, inclusive. These errors are a consequence of using the idea of access to array that actually starts at 0 and ends at last number, uniquely.

function acumula(n) {
    var total = 0;
    for (var i = 1; i <= n; i++) total += i;
    return total;
}
console.log(acumula(5));

But you don’t need any of that, you can use pure mathematics and give the result without a tie.

function acumula(n) {
    return n * (n + 1) / 2;
}
console.log(acumula(5));

I put in the Github for future reference.

  • That last one is a factorial function, isn’t it? it could be written as function acumula(n) {&#xA; if (n == 0) return n;&#xA; return acumula(n - 1) + n;&#xA;} in this way, using recursiveness?

  • That you did replaces the first code, which is that much more complicated and inefficient than my second.

  • A yes. I understood. Because of the refusal, this function would have more processing cost.

  • 1

    @Cmtecardeal, in addition, Javascript (currently) has a limit to the maximum number of calls on call stack, then, depending on the number the guy passes, the JS won’t even be able to perform successfully. I made a related question for a while, but no response.

  • 3

    @Luizfelipe unless it has an artificial limit is supposed to be a very large number because it would only be the problem of the battery bursting.

  • 1

    @Luizfelipe I understood your point, I think that when it comes to JS, I came to the conclusion that, even though I like functional programming, it does not always meet all situations. Now thanks to your comment, I learned that sometimes an impertinent way excels in relation to PF, because I confess that I did not know this problem of call stack for recursive functions. In this case I think it enters the experience of each developer in choosing which and best way to solve a problem efficiently :)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.