Javascript conditional function

Can anyone tell me the best way to go about this. I have a grid and along the x axis is a weight range for each column 81-90lbs 91-100lbs 101-110lbs… … … … Along the y axis I will have a range that represents kidney function and they will have the same 10 range between rows. Now in each x, y grid there will be a fixed dose of medicine that matches up for weight and kidney function. If the person weighs 105lbs and there kidney function is a certain number then they will get xxx mg of drug.

What is the best way to set this function up in javascript so when the person plugs in the weight and kidney function in a form it goes through to see if each value falls within each weight and kidney function range and then matches the drug dose that falls in that particular grid and gives the result. A long list of if statements or is there a way to set this grid up as an array and loop through it?

I would appreciate any input that anyone has.

Thank you,
Brad

Hello, Brad!

I made a demo on jsFiddle: http://jsfiddle.net/N8mvY/
Check it out, if it’s what you need, super! Else, can you specify in details more about the task?
Or if something you don’t understand, feel free to ask questions. :slight_smile:

volter,
Thanks for taking the time to do what you did with the code. Here is an attached image of the grid I was talking about. The ranges are a bit different then what I described. I will try and edit them in the code you worked up. .

Looking at the code you submitted I can tell a majority of whats going on. My javascript skills are basic so I would like to rundown what you did. I think I changed the weight and kidney values to match my attached grid.

This block is where you are grabbing the elements from the forms and declaring the variables.


// Alias
function getId (id) {
    return document.getElementById(id);
}

// HTML elements
// Inputs of weight and height
var weight = getId('weight'),
    kidney = getId('kidney'),
    compute = getId('compute'),
    result = getId('result');

This is where you are setting up the grid for the x (weight) and y (kidney) columns. What does the last else if statement do “else if (x && y) {data.value[x * y] = 10 * x * y;” ?


// Data initalization
data = {
    'weight': [],
    'kidney': [],
    'value' : []
};

var x, count = 10
for (x = 0; x < count; x++) {
    var y;
    for (y = 0; y < count; y++) {
        // x == 0 && y == 0
        if (y == 0 && x) {
            // Weight

            // Range
            data.weight.push({
                min: 50 + 10 * x,
                max: 50 + 9 + 10 * (x-1)
            });
        }
        else if (x == 0 && y) {
            // Kidney

            // Change to your own function, because I don't sure what function that you need, so it'll be pretended value.
            data.kidney.push({
                min: 20 + 10 * x,
                max: 20 + 9 + 10 * (x-1)
            });
        }
        else if (x && y) {
            data.value[x * y] = 10 * x * y;
        }
    }
}

console.log(data);

With this code you are validating the inputs to make sure there is a number there.


compute.addEventListener('click', function (e) {
    // Just in case
    e.preventDefault();

    var wei = weight.value,
        kid = kidney.value;

    if (!kidney || !weight) {
        alert('Empty field, kidney or weight!');

        return;
    }

    console.log(wei, kid);

This last section is where I get lost.


    var k = data.kidney.indexOf(+kid) + 1;
    var w = 0;

    data.weight.forEach(function (v, i) {
        if (wei >= v.min && wei <= v.max) {
            w = i + 1;

            return;
        }
    });

    // Result
    var r = data.value[k * w];

    console.log(k, w, r)

    if (r) {
        result.innerHTML = r;
    }
});

My last question is where do I set up my doses that are on the grid?

Thanks for going above and beyond answering my problem
Brad

Brad,

This is where you are setting up the grid for the x (weight) and y (kidney) columns. What does the last else if statement do “else if (x && y) {data.value[x * y] = 10 * x * y;” ?

Code:

else if (x && y)  {
	data.value[x * y] = 10 * x * y;
}

setting up test values for grid of dose of medicine.

With this code you are validating the inputs to make sure there is a number there.

I’m validating if it not empty, because <input type=“number”/> already doing it for me.

This last section is where I get lost.

This section where I getting x and y for values.

My last question is where do I set up my doses that are on the grid?

You can fill data.values with your own values, however, you need to fill this array with values horizontally, like that:

data.value = ["750mg q24h", "750mg q24h", "750mg q24h", "1000mg q24h", "1000mg q24h", "1250mg q24h", "1250mg q24h", "1000mg q24h"]; // and so on