I have a form on a website where a user can put in a width and a height and based on this it should return a price in a CSV. The price table looks like this. I receive the data in an array like below.
For example if I enter a height (row 1) of 850 and a width (column 1) of 660 I should receive a value of €124.04. Can anyone help point me in the right direction of how to solve this?
[
', 600, 900, 1200, 1500, 1800, 2100, 2400, 2700, 3000',
'450, €89.80, €106.26, €122.72, €139.18, €155.64, €172.10, €188.56, €205.03, €221.49',
'650, €102.97, €124.04, €145.10, €166.17, €187.23, €208.30, €229.36, €250.42, €271.49',
'850, €116.15, €141.82, €167.49, €193.15, €218.82, €244.49, €270.15, €295.82, €321.49',
'1050, €129.33, €159.60, €189.87, €220.14, €250.41, €280.68, €310.95, undefined, undefined',
'1200, €139.21, €172.93, €206.65, €240.38, €274.10, €307.82, undefined, undefined, undefined',
'1400, €152.39, €190.71, €229.04, €267.36, €305.69, undefined, undefined, undefined, undefined',
'1600, €165.56, €208.49, €251.42, €294.35, undefined, undefined, undefined, undefined, undefined',
'1800, €178.74, €226.27, €273.80, undefined, undefined, undefined, undefined, undefined, undefined',
'2000, €191.92, €244.05, €296.19, undefined, undefined, undefined, undefined, undefined, undefined',
'2200, €205.09, €261.83, undefined, undefined, undefined, undefined, undefined, undefined, undefined',
'2400, €218.27, €279.61, undefined, undefined, undefined, undefined, undefined, undefined, undefined',
'2600, €231.45, €297.39, undefined, undefined, undefined, undefined, undefined, undefined, undefined',
'2800, €244.62, €315.17, undefined, undefined, undefined, undefined, undefined, undefined, undefined',
'3000, €257.80, €332.95, undefined, undefined, undefined, undefined, undefined, undefined, undefined',
'3200, €270.98, €350.73, undefined, undefined, undefined, undefined, undefined, undefined, undefined'
]
First, you cut up each row in to cells to make a two dimensional array (representing the table):
rows.map(row => row.split(", "))
Then, you define how to pick the right height and width. This isn't 100% specified from your example. If the number requested doesn't exactly match your table, what do you do?
I chose to go for the next size up and throw an error if the requested size is too big:
const col = rows[0].findIndex(v => Number(v) >= height);
if (col === -1) throw "Height not available";
const row = rows.findIndex(r => Number(r[0]) >= width);
if (row === -1) throw "Width not available";
Then, you can query your table for the answer:
const price = rows[row][col];
Here's an interactive snippet you can try out:
const getPrice = (height, width, rows) => {
const col = rows[0].findIndex(v => Number(v) >= height);
if (col === -1) throw "Height not available";
const row = rows.findIndex(r => Number(r[0]) >= width);
if (row === -1) throw "Width not available";
const price = rows[row][col];
if (price === "undefined") throw "Combination not available";
return price;
}
console.log(
`850h 660w: ${getPrice(850, 660, getRows())}`
);
function getRows() { return [
', 600, 900, 1200, 1500, 1800, 2100, 2400, 2700, 3000',
'450, €89.80, €106.26, €122.72, €139.18, €155.64, €172.10, €188.56, €205.03, €221.49',
'650, €102.97, €124.04, €145.10, €166.17, €187.23, €208.30, €229.36, €250.42, €271.49',
'850, €116.15, €141.82, €167.49, €193.15, €218.82, €244.49, €270.15, €295.82, €321.49',
'1050, €129.33, €159.60, €189.87, €220.14, €250.41, €280.68, €310.95, undefined, undefined',
'1200, €139.21, €172.93, €206.65, €240.38, €274.10, €307.82, undefined, undefined, undefined',
'1400, €152.39, €190.71, €229.04, €267.36, €305.69, undefined, undefined, undefined, undefined',
'1600, €165.56, €208.49, €251.42, €294.35, undefined, undefined, undefined, undefined, undefined',
'1800, €178.74, €226.27, €273.80, undefined, undefined, undefined, undefined, undefined, undefined',
'2000, €191.92, €244.05, €296.19, undefined, undefined, undefined, undefined, undefined, undefined',
'2200, €205.09, €261.83, undefined, undefined, undefined, undefined, undefined, undefined, undefined',
'2400, €218.27, €279.61, undefined, undefined, undefined, undefined, undefined, undefined, undefined',
'2600, €231.45, €297.39, undefined, undefined, undefined, undefined, undefined, undefined, undefined',
'2800, €244.62, €315.17, undefined, undefined, undefined, undefined, undefined, undefined, undefined',
'3000, €257.80, €332.95, undefined, undefined, undefined, undefined, undefined, undefined, undefined',
'3200, €270.98, €350.73, undefined, undefined, undefined, undefined, undefined, undefined, undefined'
].map(r => r.split(", ")); }