简体   繁体   中英

How to calculate holes with 3 parameters dependent on each other with JS

I need to create simple program who calculate user input data. User need to chose 3 parameters - size, pressure, and units. Size and pressure are from html select option and units are from input . I can't figure out how to start my calculation code...

Example:

size = 100, pressure = 6, units = 10 /// size 100 in pressure 6 have 8 holes with size M8, so the result is 8*10 = 80 holes M8.

size = 100, pressure = 10, units = 10 /// size 100 in pressure 10 have 10 holes with size M8, so the result is 10*10 = 100 holes M8.

size 150, pressure = 10, units = 10 /// size 150 in pressure 10 have 12 holes, with size M10, so the result is 10*10 = 100 holes M10.

end etc.

Size of holes depends of input size.

How many holes depends of pressure.

I need guidance how to start my calculation code. I tried with loops and switch case, but is a lot of code to list sizes from 50 1000, pressure from 4 to 20.

To achieve expected result, use below option of creating one object like properties and use it with function for calculations based on parameters

  1. Create object with size, pressure and corresponding multiplier
  2. Create function with parameters - size, pressure, units
  3. Return calculated value based on parameters passed to function with values from properties object
  4. Flexible to add new sizes as new object to properties object instead of creating new switch case or updating switch case

 var properties = { 50: { '6': {multiplier: 4}, '10': {multiplier: 6}, '16': {multiplier: 10}, 'na': {multiplier: 12} }, 80: { '6': {multiplier: 6}, '10': {multiplier: 8}, '16': {multiplier: 20}, 'na': {multiplier: 24} }, 100: { '6': {multiplier: 8}, '10': {multiplier: 10}, '16': {multiplier: 20}, 'na': {multiplier: 24} }, 150: { '6': {multiplier: 8}, '10': {multiplier: 10}, '16': {multiplier: 16}, 'na': {multiplier: 26} } } function calculate(size, pressure, units){ return properties[size][pressure] ? properties[size][pressure].multiplier* units : properties[size]['na'].multiplier * units ; } console.log("size = 100, pressure = 6, units = 10, caculated holes --> ", calculate(100, 6, 10)); // 80 console.log("size = 100, pressure = 10, units = 10, caculated holes --> ", calculate(100, 10, 10)); //100 console.log("size = 150, pressure = 10, units = 10, caculated holes --> ", calculate(150, 10, 10)); // 100 

codepen - https://codepen.io/nagasai/pen/VVozqw?editors=1010

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM