Assistance with switching variables

The variable room_x_beds is needed, I'm not worried about the default scope.

Can't I assign var room_x_beds to var x?

I do not understand why.

Code:

var room_1_beds = 2;
var room_2_beds = 2;
var room_3_beds = 1;
var give_num = prompt();
var x;

switch(give_num){
    case "1":
        x = room_1_beds;
        break;
    case "2":
        x = room_2_beds;
        break;
    case "3":
        x = room_3_beds;
        break;
    default:
    break;
        }

switch(x){
    case "1":
        console.log ("text");
    break;
    case "2":
        console.log ("text");
    break;
    default:
    break;
}

      

+3


source to share


1 answer


The values ​​of the second operator switch

must be numbers, not lines.



var room_1_beds = 2;
var room_2_beds = 2;
var room_3_beds = 1;
var give_num = prompt();
var x;
switch(give_num){
    case "1":
        x = room_1_beds;
        break;
    case "2":
        x = room_2_beds;
        break;
    case "3":
        x = room_3_beds;
        break;
    default:
    break;
        }

switch(x){
    case 1:
        console.log ("text");
    break;
    case 2:
        console.log ("text");
    break;
    default:
    break;
}
      

Run codeHide result


+3


source







All Articles