-1

I am working on an assignment and have come across an issue. I am trying to write and if, else statement. The issue is that the else statement is not being ran. Here is my work:

var age = "2"; //given age
var floorType = "floor"; //given floor (floor or balcony)
var floorS = "10"; 
var balconyS = "6"; 

//toddler1
if (floorType = "floor" && age <= 4) {
    print ("Floor Ticket:" + " "+ "Free"); 
} else if (floorType = "balcony" && age <=4) {
    print ("Balcony Ticket:" + " "+ "Free");
}

When to code is executed you can see that the else statement won't work and it just prints the data from the first if statement.

Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
bluereino
  • 11
  • 4

2 Answers2

1

When using conditionals such as "if" and "else" statements, you need to use "===" instead of "="

"=" is assigning your variable instead of comparing.

So you code should instead be -

var age = "2"; //given age
var floorType = "floor"; //given floor (floor or balcony)
var floorS = "10"; 
var balconyS = "6"; 

//toddler1
if (floorType === "floor" && age <= 4) {
    print ("Floor Ticket:" + " "+ "Free"); 
} else if (floorType === "balcony" && age <=4) {
    print ("Balcony Ticket:" + " "+ "Free");
}
addybist
  • 461
  • 1
  • 4
  • 13
0

Your if condition needs correction. See below.

var age = "2"; //given age
var floorType = "floor"; //given floor (floor or balcony)
var floorS = "10"; 
var balconyS = "6"; 

//toddler1

if (floorType === "floor" && age <= 4) {
    print ("Floor Ticket:" + " "+ "Free"); 
} else if (floorType === "balcony" && age <=4) {
    print ("Balcony Ticket:" + " "+ "Free");
}

You need to replace = with === or ==.

= is for assignment, like when you want to assign a value to a variable. === or == is for equality, like when you want to see if a variable is already equal to something.

Matt Croak
  • 2,788
  • 2
  • 17
  • 35