var tomr = 'rain';
if(tomr == 'rain')
{
console.log('Take a raincoat');
} else {
console.log('No need to take a raincoat');
}
Write a program that works out whether if a given year is a leap year or not?
var year = 2004;
if(year % 4 === 0){
if(year % 100 === 0){
if(year % 400 === 0){
console.log('Leap year');
} else {
console.log('Not a leap year');
}
}
else {
console.log('Leap year');
}
} else {
console.log('Not a leap year');
}
if(score = 0) { // we are comparing, we are assigning and 0 is false.
console.log("OMG, we loss the game");
} else {
console.log("OMG, we won the game");
}
variableName = (condition) ? value1:value2
var age = 16;
console.log((age >= 18) ? "You can vote" : "You can't vote");
1st without using switch find the area of circle, triangle and rectangle.
var shape = 'circle'
if(area == "circle"){
console.log("the area of the circle is : " + PI*r**2);
}else if(area == "triangle"){
console.log("the area of the triangle is : " + (l*b)/2);
}else if(area == "rectangle"){
console.log("the area of the rectangle is : " + (l*b));
}else{
console.log("please enter valid shape");
}
Using switch case
var shape = 'circle' ;
var PI = 3.142, l=5, b=4, r=3;
switch(shape){
case 'circle':
console.log("the area of the circle is : " + PI*r**2);
break;
case 'triangle':
console.log("the area of the triangle is : " + (l*b)/2);
break;
case 'rectangle':
console.log("the area of the rectangle is : " + (l*b));
break;
default:
console.log("please enter valid shape");
}
var num = 20;
while(num <= 10) {
console.log(num); // infinite loop
num++;
// num--;
}
var num = 5;
do {
console.log(num);
num++;
} while (num <= 10)
for(var num = 0; num <= 10; num++)
{
console.log(num);
}
JavaScript program to print table for a given number (8)?
var tableof = 8
for(var i = 1; i <= 10; i++) {
console.log(tableof + " * " + i + " = " + tableof * i);
}