patternjavascriptMinor
Calculate torque of a bolt based on grade, size, and lubricant
Viewed 0 times
torquesizegradeandcalculatebasedlubricantbolt
Problem
To calculate torque I need to look up a value from a table which I've hardcoded based on three conditions: grade, size, and thread. Then I need to multiply it by coefficients associated with the selected lubricants.
I've written the code to be a web-app, but it will also just run as an html file sitting on a company drive, not connected to the internet. Because it's offline, I needed to hardcode the values instead of look them up from a data file because of same-origin policy and JavaScript security settings. It's not ideal style, but I couldn't think of an alternative.
It works, but I want to make sure it's really robust and easy for anyone to maintain after my internship is done. It's one of the first programs I've written in this language and I don't get easily offended, so please be thorough! I'm not sure what's considered good style or efficient. Also, this needs to be run on IE8 so some things I've done have reflected the limitations of that browser.
JSFiddle
JavaScript
```
var size;
var grade;
var thread;
var value;
document.getElementById("message").innerHTML = "Select grade.";
function fetch_grade_sizes() {
// get grade from select menu
var grade_menu = document.getElementById("grade_menu");
grade = grade_menu.options[grade_menu.selectedIndex].value;
// get thread from radio buttons
var radios = document.getElementsByName("radio");
for (var i = 0, length = radios.length; i ";
}
}
// round the resulting value to one decimal place
value = Math.round((torque check_number) Math.pow(10, 1)) / Math.pow(10, 1);
var value_in = Math.round((torque check_number 12) * Math.pow(10, 1)) / Math.pow(10, 1);
var value_SI = Math.round((torque check_number 1.35581795) * Math.pow(10, 1)) / Math.pow(10, 1);
// display the multiplication string and the result
I've written the code to be a web-app, but it will also just run as an html file sitting on a company drive, not connected to the internet. Because it's offline, I needed to hardcode the values instead of look them up from a data file because of same-origin policy and JavaScript security settings. It's not ideal style, but I couldn't think of an alternative.
It works, but I want to make sure it's really robust and easy for anyone to maintain after my internship is done. It's one of the first programs I've written in this language and I don't get easily offended, so please be thorough! I'm not sure what's considered good style or efficient. Also, this needs to be run on IE8 so some things I've done have reflected the limitations of that browser.
JSFiddle
JavaScript
```
var size;
var grade;
var thread;
var value;
document.getElementById("message").innerHTML = "Select grade.";
function fetch_grade_sizes() {
// get grade from select menu
var grade_menu = document.getElementById("grade_menu");
grade = grade_menu.options[grade_menu.selectedIndex].value;
// get thread from radio buttons
var radios = document.getElementsByName("radio");
for (var i = 0, length = radios.length; i ";
}
}
// round the resulting value to one decimal place
value = Math.round((torque check_number) Math.pow(10, 1)) / Math.pow(10, 1);
var value_in = Math.round((torque check_number 12) * Math.pow(10, 1)) / Math.pow(10, 1);
var value_SI = Math.round((torque check_number 1.35581795) * Math.pow(10, 1)) / Math.pow(10, 1);
// display the multiplication string and the result
Solution
A few things caught my eye here.
Firstly, since you need to store all the data in the files (which, I agree, is the best way to go about this, given the constraints) why not store all the data there? By which I mean build the lubricant list and grades in JS too. Or flip it around and keep everything in the HTML (hiding and showing the appropriate dropdowns as needed). Eitherway, I think it'd be better to keep all the source data in one place, rather than split it up. This will make the code a lot more maintainable.
And if you include the "select only one type of lubricant" constraint you've talked about in the comments, everything can be done as single-selection dropdowns, making the work of building the UI simpler. You just need a
I'd probably choose to define the values in a separate JS file and load that in the html. Loading a local JS file with a
Firstly, since you need to store all the data in the files (which, I agree, is the best way to go about this, given the constraints) why not store all the data there? By which I mean build the lubricant list and grades in JS too. Or flip it around and keep everything in the HTML (hiding and showing the appropriate dropdowns as needed). Eitherway, I think it'd be better to keep all the source data in one place, rather than split it up. This will make the code a lot more maintainable.
And if you include the "select only one type of lubricant" constraint you've talked about in the comments, everything can be done as single-selection dropdowns, making the work of building the UI simpler. You just need a
buildDropdown function.I'd probably choose to define the values in a separate JS file and load that in the html. Loading a local JS file with a
-
Don't mess around with multiplying and dividing and rounding to get a specific number of decimals. Just use number.toFixed(numberOfDecimals)`Code Snippets
selectList.setAttribute("onclick", "calculate(this.form);");selectList.onclick = function () { calculate(this.form) };selectList.onclick = calculate;Context
StackExchange Code Review Q#56573, answer score: 5
Revisions (0)
No revisions yet.