Java script 1 Write a conditional statement that outputs the
(Java script)
1.) Write a conditional statement that outputs the smaller value of the two values stored in integer variables, x and y.
2.) Write code that adds 7 to a variable if it is even and subtracts 7 from it if it is odd.
Solution
Please find the required program along with its output. Please see the comments against each line to understand the step.
1)
x = 15 //initialize x with some value
y = 92 //initialize y with some value
if (x < y) { //conditional statement which determines the smaller value of x and y
small = x; //if x is less than y, then small is x
} else {
small = y; //if y is less than x, then small is y
}
console.log(\"small =\", small) //print the small value among x and y in console
-------------------------------
OUTPUT:
2)
x = 10 //initialize x with some value
console.log(\"Before : x =\", x) //print the initial value of x in console
if (x % 2 == 0) { //conditional statement which determines whether x is even (if reminder of x/2 ==0, means divisible by 2, which means even numner)
x = x + 7; //if x even, then add 7 to x
} else {
x = x - 7; //if x is odd, then substract 7 from x
}
console.log(\"After : x =\", x) //print the final value of x in console
--------------------------------------------------------
OUTPUT:
