* If / else statements
*/

var firstName = 'Rajat Bose';
var civilStatus = 'single';

if (civilStatus === 'married') {
    console.log(firstName + ' is married!');
} else {
    console.log(firstName + ' will hopefully marry soon :)');
}


var isMarried = true;
if (isMarried) {
    console.log(firstName + ' is married!');
} else {
    console.log(firstName + ' will hopefully marry soon :)');
}

var massMark = 78; // kg
var heightMark = 1.69; // meters

var massRajat = 92;
var heightRajat = 1.95;

var BMIMark = massMark / (heightMark * heightMark);
var BMIRajat = massRajat / (heightRajat * heightRajat);

if (BMIMark > BMIRajat) {
    console.log('Mark\'s BMI is higher than Rajat\'s.');
} else {
    console.log('Rajat\'s BMI is higher than Marks\'s.');
}




/*****************************
* Boolean logic
*/

var firstName = 'Rajat';
var age = 20;

if (age < 13) {
    console.log(firstName + ' is a boy.');
} else if (age >= 13 && age < 20) {
    console.log(firstName + ' is a teenager.');
} else if (age >= 20 && age < 30) {
    console.log(firstName + ' is a young man.');
} else {
    console.log(firstName + ' is a man.');
}




/*****************************
* The Ternary Operator and Switch Statements
*/
/*
var firstName = 'Rajat';
var age = 14;

// Ternary operator
age >= 18 ? console.log(firstName + ' drinks beer.') : console.log(firstName + ' drinks juice.');

var drink = age >= 18 ? 'beer' : 'juice';
console.log(drink);

(if (age >= 18) {
    var drink = 'beer';
} else {
    var drink = 'juice';
}

// Switch statement
var job = 'instructor';
switch (job) {
    case 'teacher':
    case 'instructor':
        console.log(firstName + ' teaches kids how to code.');
        break;
    case 'driver':
        console.log(firstName + ' drives an uber in Lisbon.');
        break;
    case 'designer':
        console.log(firstName + ' designs beautiful websites.');
        break;
    default:
        console.log(firstName + ' does something else.');
}

age = 56;
switch (true) {
    case age < 13:
        console.log(firstName + ' is a boy.');
        break;
    case age >= 13 && age < 20:
        console.log(firstName + ' is a teenager.');
        break;
    case age >= 20 && age < 30:
        console.log(firstName + ' is a young man.');
        break;
    default:
        console.log(firstName + ' is a man.');
}




/*****************************
* Truthy and Falsy values and equality operators
*/
/*
// falsy values: undefined, null, 0, '', NaN
// truthy values: NOT falsy values

var height;

height = 23;

if (height || height === 0) {
    console.log('Variable is defined');
} else {
    console.log('Variable has NOT been defined');
}

// Equality operators
if (height === '23') {
    console.log('The == operator does type coercion!');
}

/*****************************
* Functions
*/
/*

function calculateAge(birthYear) {
    return 2020 - birthYear;
}

var ageRajat = calculateAge(1990);
var ageSukrit = calculateAge(1948);
var ageBinod = calculateAge(1969);
console.log(ageRajat, ageSukrit, ageBinod);


function yearsUntilRetirement(year, firstName) {
    var age = calculateAge(year);
    var retirement = 65 - age;
    
    if (retirement > 0) {
        console.log(firstName + ' retires in ' + retirement + ' years.');
    } else {
        console.log(firstName + ' is already retired.')
    }
    
}

yearsUntilRetirement(2046, 'Rajat');
yearsUntilRetirement(2048, 'Sukrit');
yearsUntilRetirement(2069, 'Binod');

Please follow and like us:

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

RSS
Follow by Email