javascript - JS: While loop to find the product of consecutive integers -


all!

i'm new programming , trying write while loop return product of numbers 1 n, inclusive. can't code work properly; keeps returning weird numbers.

i think problem first line of while loop. it's it's not multiplying, don't know why.

here code wrote:

var n = 7; var multiplier = 1;  while (multiplier <= n){     multiplier = (multiplier * multiplier+1);      if (n < 6){         multiplier+= 2;     }     else {         multiplier++;     }; };  console.log(multiplier); 

the problem use of multiplier variable, using store result, instead need use separate variable store result , use counter variable like

var n = 5;  var multiplier = 1;      var result = 1;  while (multiplier <= n) {    result *= multiplier++;    };    document.body.innerhtml = (result);


if @ loop below, @ end of 1st iteration multiplier 3, end of second loop 11 higher 7 loop exists.

var n = 7;  var multiplier = 1;      while (multiplier <= n) {    multiplier = (multiplier * multiplier + 1);      if (n < 6) {      multiplier += 2;    } else {      multiplier++;    };    snippet.log('loop: ' + multiplier)  };    snippet.log('result: ' + multiplier);
<!-- show result in dom instead of console, used in snippet not in production -->  <!-- provides `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->  <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -