Jon Kantner
Jon Kantner

Reputation: 603

Store Math Formula in Variable

My script can print a pattern of triangular numbers (e.g. 1, 3, 6, 10,...), but I want to store the formula (n * (n + 1)) / 2 in a variable called formula instead of writing it multiple times in the if-else statement.

/* Current script */

var pat = document.getElementById("pattern");
var nMax = 10; // max number of intervals

for (var n = 1; n <= nMax; ++n) {
    if (n != nMax)
        // insert comma and space after each interval
    	pat.innerHTML += (n * (n + 1)) / 2 + ", ";
    else
        // append ellipsis after last interval
        pat.innerHTML += (n * (n + 1)) / 2 + ",&hellip;";
}
<body>
    <p id="pattern"></p> 
</body>

Here is what I tried already but ended up with 1, 1, 1, 1, 1...:

I moved var n = 1 to its own line under the nMax variable so that n is defined in the formula and changed the first part of the for loop to n = 1. Then in the if-else statement, I replaced every instance of (n * (n + 1)) / 2 with the formula variable. After testing the modified script, the pattern resulted in all 1s.

/* Version with formula variable (not working properly) */

var pat = document.getElementById("pattern");
var nMax = 10; // max number of intervals
var n = 1;

var formula = (n * (n + 1)) / 2;

for (n = 1; n <= nMax; ++n) {
    if (n != nMax)
    // insert comma and space after each interval
      pat.innerHTML += formula + ", ";
    else
    // append ellipsis after last interval
      pat.innerHTML += formula + ",&hellip;";
}
<body>
    <p id="pattern"></p> 
</body>

How could I store the formula in a variable and use it without getting unexpected results?

Upvotes: 4

Views: 2833

Answers (4)

pawel
pawel

Reputation: 36975

What you're trying to create is a function, and you were quite close in your attempts.

To create a function and assign it to a variable you write

var formula = function( arguments ){ /* function statements */ }

In your case it would be

var formula = function(n){ return (n * (n + 1)) / 2; }

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function

Or, to make things less boring, you could use ES2015 (EcmaScript 6) arrow function expression. Keep in mind it's a new feature and not supported across all browsers, but it's the direction JavaScript is headed in near future.

var formula = (n) => (n * (n + 1)) /2;

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Upvotes: 1

Screech129
Screech129

Reputation: 110

You could make your formula variable a function that takes in an integer "n".

var pat = document.getElementById("pattern");
var nMax = 10; // max number of intervals
var n = 1;

function formula(n){
 return (n * (n + 1)) / 2};
for (n = 1; n <= nMax; ++n) {
    if (n != nMax)
    // insert comma and space after each interval
      pat.innerHTML += formula(n) + ", ";
    else
    // append ellipsis after last interval
      pat.innerHTML += formula(n) + ",&hellip;";
}
<body>
    <p id="pattern"></p> 
</body>

Upvotes: 2

juvian
juvian

Reputation: 16068

A variable is evaluated on the fly, so if you want to change the parameters later on, you need to store it as a function:

var formula = function(n){return (n * (n + 1)) / 2;}

//and then you can use
pat.innerHTML += formula(n)

This makes formula a reference to the function that takes a number and returns the formula you wanted applied to that number. Then you can call it using formula(n), where n is the number you want to apply the function to

Upvotes: 7

Pointy
Pointy

Reputation: 413846

Put the formula in a function:

function formula(n) { return n * (n + 1) / 2; }

Then you can call it :

pat.innerHTML = formula(n) + ", ";

Upvotes: 7

Related Questions