0

I’m trying to make a simple script for a retailer with a webpage – we have three Fruits – Apple , Banana, Orange

Two Qualities each – Apple ( bad and good) , Banana( bad & good) and Orange (bad and good)... Bad in all cases an inferior quality.

So I have two quality options -- good and bad

Quantity from 1 to 10

I did something like this with array but it is only applicable to two variable not three . Final result I want is –

I chose Apple > good > 5 units = 5*4 = 20

Form Option - select (apple, banana, orange), select(good, bad) , select(number of units)

Here is what I have done

    costperfruit = new Array(4,5,6, 7,8,9);

    function setcost()
    {
        cpp = costperfruit[document.frm.colour_fruit.value*1-1];
        sum = cpp*document.frm.numberUnits.value;
        sum += "";
        pos = sum.indexOf(".");
        if (pos>0)
        {
            sum = sum.substring(0,pos+3);
            if (sum.indexOf(".")+3>sum.length)
                sum += "0";
        }
        else
            sum += ".00";

        document.frm.cost_per_fruit.value = cpp;
        document.frm.total_sum.value = sum;
    }

With this I can easily multiply number of units with cost of fruits. Green option is inferior quality in each case thus sells for 3,5,7 , One USD less than red, yellow, red orange

I want to see price when I choose --

orange> bad> 3 = 7*3 = 21 apple> bad > 4 units > 3*4 = 12

I'm using select form in HTML.

2 Answers 2

1

Your solution can work, but it is hard to maintain it. I recommend you to use some data structure to store product prices:

var prices = {
    "apple": {
        "good": 4,
        "bad": 5
    }
    "banana": {
        "good": 6,
        "bad": 7
    }
    "orange": {
        "good": 8,
        "bad": 9
    }
}

Then if you want to select price for specific item you simply use:

prices["apple"]["bad"]
Sign up to request clarification or add additional context in comments.

1 Comment

Let me run it and try to fix in my solution with html tags. Will certainly let you know. I've have very little experience with javascript.
0

Expanding on suvroc's suggestion above, I added a bit of code to process the Object Literal.

Take a look at the following Fiddle and see if it helps.

https://jsfiddle.net/f79vj0s3/

Note that I changed the Object:

// Object Literal
var produce = {
    "Apple": {
        "good": 
            {
            "qty" : 5, 
            "cost" : 2
          },
        "bad": 
            {
            "qty" : 3, 
            "cost" : 1
          }
    },
    "Banana": {
        "good": 
            {
            "qty" : 6, 
            "cost" : 3
          },
        "bad": 
            {
            "qty" : 4, 
            "cost" : 2
          }
    },
    "Orange": {
        "good": 
            {
            "qty" : 5, 
            "cost" : 3
          },
        "bad": 
            {
            "qty" : 3, 
            "cost" : 1
          }
    }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.