0

I'm studying dropdown box in JS.

This JS worked so well but I found out some calc doesn't work. so I've adding code and trying to fix.

The program movement is... User select 3 dropdown box, Then display result. User will select '30' or '40' or '50' firstly. After that select 'type' Finally User can select 'product'. Product lists are already decided by "1st and 2nd select value".

This program run perfectly Only when user selected value '30'

I would like to add calc CASE '40' and '50' .

For example

If user select

tubosuValue == 30 && u_typeValue == 'M' && productData == 920

result will 920

if user select

tubosuValue == 50 && u_typeValue == 'S' && productData == 1593

result will be 1593

I put whole code here

I wrote code below but It looks ugly. I couldn't figure out good way.

Could you teach me right code please?

JS part

let result = document.getElementById("result");
if (tubosuValue && u_typeValue && productValue) {
    //if all value selected

    if(tubosuValue == 30 && u_typeValue == 'M' && productValue == 920) {
    hoge = 920;
    }
    if(tubosuValue == 30 && u_typeValue == 'M' && productValue == 1117) {
    hoge = 1117;
    }
    if(tubosuValue == 30 && u_typeValue == 'M' && productValue == 1180) {
    hoge = 1180;
    }
    if(tubosuValue == 40 && u_typeValue == 'M' && productValue == 1182) {
    hoge = 1182;
    if(tubosuValue == 50 && u_typeValue == 'S' && productValue == 1593) {
    hoge = 1485;

    }

    result.value = ValueUtils.comma(hoge);

UPDATE I try to write question simple

I rewrite as easy Here is my goal

First User select 30 or 40 or 50 value (Green area)

Second User select M , S ,K , AL (blue area)

Finally User choose Red area.

If user select 40 and K , the User only can select p1 and p2 value.

if user select 40 and k and p1 result will be 21

If user select 40 and K and p2 result will be 22

[![enter image description here][1]][1]

1 Answer 1

1

This is still not "the best" solution because I dont really know what your trying to achieve and how it can be done better, but its way better then 100 if statements.

const tubosuValueArray = [30,40,50];
const productValueArray = [920, 1117, 1180, 1182];
const typeValue = 'M';
if (tubosuValueArray.includes(tubosuValue) && 
    typeValue === u_typeValue) && productValueArray.includes(productValue)) {
  hoge = productValue
} else if (tubosuValue == 50 && u_typeValue == 'S' && productValue == 1593) {
  hoge = 1485
}

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for answer. I will rewrite my question soon. Could you look at that again please ?
I fixed my answer, I missed the producat value check
I update my question. I tried to write as simple as possible. Could you take a look at it please?
Thank you for helping me. I studied a lot from your answer but I would like to use my current code so I think I clean up my question and I will rewrite this question. Thank you very much again.

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.