Matthew Sirkin - 6 months ago 27

Javascript Question

I have a javascript that is generating a table for me. The elements in the table are gathered in an array of arrays called sep. Sep contains 1152 sub arrays that are of the form:

`Sep[0] //["316SS", "K", "-100 to 225°C", "Brass", "1/8", "4'", "4'", "8", "Ungrounded"]`

So basically there are 1152 rows, each of which defines a products with 9 parameters. I want to make a for-loop that will create a price for each of the configurations. This is what I have so far:

`//PART 1-------------WORKS FINE-----------------------------------`

var eopartprice2 = []; //matrix that I want to contain my prices

for (var i = 0; i < sep.length; i++) {

strnum1 = sep[i][5]; //parameter 5 is a length of material

len1 = Number(strnum1.substr(0, strnum1.length - 1));

strnum2 = sep[i][6]; //parameter 6 is another length of material

len2 = Number(strnum2.substr(0, strnum2.length - 1));

strnum3 = sep[i][7]; //parameter 7 is the number of units required

condnum = Number(strnum3.substr(0, strnum3.length));

feetOfMat = len1*len2*condnum; //The product of these is the total feet of req material

//PART 2------------PFCost always = 0.87--------------------------

//Next i need to identify the cost of the material (to multiply by the total feet)

var costOfMat = [0.87, 0.87, 1.77, 0.55] //different costs of the 4 materials

if (sep[i][0] = "304SS") {

var PFCost = costOfMat[0]; //304SS costs 0.87/foot

} else if (sep[i][0] = "316SS") {

var PFCost = costOfMat[1]; //316SS costs 0.87/foot

} else if (sep[i][0] = "Inconel") {

var PFCost = costOfMat[2]; //Inconel costs 1.77/foot

} else if (sep[i][0] = "High Temp. Glass") {

var PFCost = costOfMat[3]; //High Temp. Glass costs 0.55/foot

}

baseMatCost[i] = PFCost*feetOfMat; //I'd like to generate a matrix that

//contains all of the base prices (1 for each row)

//PART 3---------------fitcost always = 36------------------------

//Trying to identify the cost of brass vs. stainless fittings

if (sep[i][3] = "Brass") {

fitcost = 36;

} else if (sep[i][3] = "Stainless Steel") {

fitcost = 37;

}

}

My Problem so far is that I want the prices to be defined based off of whether or not the if statements are satisfied but in both cases (fitcost and PFCost) the values are simply the ones defined in the first if statement.

Lastly I'd like to generate my final price in the eopartprice2 matrix based off adding up the materials generated above + some cost of labor multiplied by some margin.

Also I'm concerned with the speed of how quickly this runs as it will be a live table in my website, and every time I add more to this I feel like it's taking longer and longer to generate. Here's a link to my w3 that I'm working in. http://www.w3schools.com/code/tryit.asp?filename=FAARB3V5GF9C

Please, any help would be greatly appreciated :)

Answer

In your if statement conditions, you're using a single equals sign. This is an assignment operator, not a comparison operator!

So, an if statement such as `if (sep[i][0] = "304SS")`

is actually assigning the value "304SS"; it is **not** comparing the value "304SS" to `sep[i][0]`

.

To correctly compare the values, you'll want to change the single equals sign to a double equals:

`if (sep[i][0] == "304SS")`

.

Note: `==`

will convert types if necessary before comparing. For example: `".87" == 0.87`

returns `true`

.

Source (Stackoverflow)