2

I hope you guys can help me!

I'm trying the following:

1000 + 100 = 1100 * 2 = 2200;
2200 + 100 = 2300 * 2 = 4600;
4600 + 100 = 4700 * 2 = 9400;
9400 + 100 = 9500 * 2 = 19000;
...

But I'm getting the flowing result.

1000 + 100 = 1100 * 2 = 2200;
2200 * 2 = 4400
4400 * 2 = 8800
... 

The values are inputed by the user, but I used static values for the example.

I have te following code:

var generate = function generate(rate){
    var array = [];
    s=0;
    for (var i = 0; i < 10; i++){
        s =+ 100;
        array.push([
            (parseInt(1000) + 100) * Math.pow(2, i),
        ]); 
    }
    return array;
}
3
  • 5
    parseInt(1000) is precisely identical to 1000. There's no need for the call to parseInt(). Also, it's +=, not =+. edit oh wait I see, 1000 is a "static value for the example". Fair enough. Commented Mar 3, 2016 at 15:05
  • Can you describe more clearly what your input, and expected output is? Do you expect an array of numbers or just one number? Commented Mar 3, 2016 at 15:05
  • You're using Math.pow(2, i) but nothing in the sample formulas you posted mention anything about exponents. Commented Mar 3, 2016 at 15:08

4 Answers 4

1

Sounds to me like you want a function that adds 100, doubles and then calls itself recursively a fixed number of times.

This function will run 10 times and output the final answer:

function add100andDouble(num, runTimes){
  if(runTimes == 0) return num;
  return add100andDouble( (num + 100 ) * 2, runTimes - 1 );
}

$(function(){
  var number = parseFloat(prompt("Enter a number"));
  alert( add100andDouble(number, 10) );
});

Sign up to request clarification or add additional context in comments.

6 Comments

This requires jQuery.
What part of this answer requires jQuery?
The $(function () { ... }) wrapper.
Ok so then just use window.onload = function(){ ... } is that really so difficult? or put it at the bottom of the body.
why would it need to be at the bottom of body, or wrapped in $() ow window.onload? does not rely on DOM readiness at all
|
1

var generate = function generate(rate){
    var array = [];
    s=0;
    for (var i = 0; i < 10; i++){
        s = rate + (array[i-1] ? array[i-1] :1000);
        array.push(s*2); 
    }
    return array;
}
console.log(generate(100));

Comments

1

I suggest to use a function f(x) for calculating the new value. It makes the code more clearly arranged.

function f(x) {
    return (x + 100) * 2;
}

var i = 0,
    x = 1000,
    array = [];

for (i = 0; i < 10; i++) {
    array.push(x);
    x = f(x);
}
document.write('<pre>' + JSON.stringify(array, 0, 4) + '</pre>');

Comments

0

Recursion has its merits on other solutions but I would go with plain old for loop for this situation.

Assuming rate is the starting number, and the return is a fixed 10 value series:

function generate(rate) {
    var series = [];
    for(var i = 0; i < 10; i++) { 
      series.push(rate = ((rate + 100) * 2));
    };
    return series;
}

It can be easily modified to send the series length if needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.