bluetail
bluetail

Reputation: 183

Selecting matched value from array in JS

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]

Upvotes: 0

Views: 61

Answers (1)

Talg123
Talg123

Reputation: 1506

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
}

Upvotes: 1

Related Questions