04 - Conditional Statements in JavaScript
04 - Conditional Statements in JavaScript
Use Case:
Solution:
A Conditional statement lets your code make decisions. It checks if something is true or
false, and then runs certain code based on that.
For Example:
"If it's raining, take an umbrella. Otherwise, enjoy the sunshine!"
Different Types of Conditional Statements
1. if Statement
2. if...else Statement
3. if...else if...else (Also called "Else-If Ladder")
4. Nested if Statements
5. switch Statement
6. Ternary Operator [? :] (short form)
1. if Statement
2. if...else Statement
3. if...else if...else (Also called "Else-If Ladder")
4. Nested if Statements
3. Switch Statement
Solution:
Challenge 2:
Solution:
Q.1- Give choice to the user to select theme color and set the selected theme color and console it.
Q.2- Find the smallest of three numbers. Numbers are given by the user.
Q.3- Write a program to manage Role-Based Access Control
Given a user role ("admin", "editor", "viewer"):
Admin: full access
Editor: edit access
Viewer: read-only
Any other: no access
Use switch.
Q.4- Check if Number is Divisible by 3 or 5 or Both. Print "Fizz" for multiples of 3,"Buzz" for multiples of 5,
"FizzBuzz" for both.
Q.5- Create a simple calculator. Take two numbers and an operator (+, -, *, /) and calculate the result using switch.
Q.6- Create a simple ATM program.
User can choose:
1. Check Balance
2. Deposit
3. Withdraw
4. Exit
Note that in case of “Deposit” if deposit amount is less than 1Rs produce error otherwise deposit the amount and show
the message with a new balance. And in case of “Withdraw” if withdraw amount is greater than balance then or less
than 1Rs then produce error otherwise withdraw amount and show remaining balance.