Exercise-01 Max Of Two Numbers
Write a function that takes two numbers and returns the maximum of the two
let number = max(2, 10);
console.log(number);
function max(number1, number2) {
/*if (number1 > number2)
return number1;
else
return number2;
*/
return (number1 > number2) ? number1 : number2;
}
Exercise-02 Landscape or Portrait
console.log(isLandscape(800, 600));
function isLandscape(width, height){
return (width > height);
}
Exercise-03 FizzBuzz
Divisible by 3 Fizz
Divisible by 5 Buzz
Divisible by both 3 and 5 FizzBuzz
Not Divisible 3 or 5 input
typeof NaN
Output "number"
const output = fizzBuzz(7);
console.log(output);
function fizzBuzz(input) {
if (typeof input !== 'number')
return NaN;
if ((input % 3 === 0) && (input % 5 === 0))
return 'FizzBuzz';
if (input % 3 === 0)
return 'Fizz';
if (input % 5 === 0)
return 'Buzz';
return input;
}
Exercise-04 Demerit Points
Speed Limit = 70
checkSpeed(50);
function checkSpeed(speed) {
const speedLimit = 70;
const kmPerPoint = 5;
if (speed < speedLimit + kmPerPoint) {
console.log('OK');
return ;
}
const points = Math.floor((speed - speedLimit) / kmPerPoint);
if (points >= 12)
console.log('License suspended');
else
console.log('Points ', points);
}
Exercise-05 Even and Odd numbers
showNumbers(10);
function showNumbers(limit) {
for (let i = 0; i <= limit; i++) {
/*if (i % 2 === 0)
console.log(i, 'EVEN');
else
console.log(i, 'ODD');*/
const message = (i % 2 === 0) ? 'EVEN' : 'ODD';
console.log(message);
}
}
Exercise-06 Count Truthy
const array = [0, null, undefined, '', Nan, false, 2, 9];
console.log(countTruth(array));
function countTruthy(array) {
let count = 0;
for (let value of array) {
if (value)
count++;
}
return count;
}
Exercise-07 String Properties
const movie = {
title: 'Mustang',
releaseYear: 2020,
rating: 4.5,
director: 'Unity'
};
showProperties(movie);
function showProperties(obj) {
/*for (let key in obj)
console.log(key);*/
for (let key in obj) {
if (typeof obj[key] === 'string')
console.log(key, obj[key]);
}
}
Exercise-08 Sum of Multiples of 3 and 5
Multiples of 3: 3, 6, 9
Multiples of 5: 5, 10
sum = 3+6+9+5+10 = 33
console.log(sum(10));
function sum(limit) {
let sum = 0;
for (let i = 0; i <= limit; i++) {
if (i % 3 === 0 || i % 5 === 0)
sum += i;
}
return sum;
}
Exercise-09 Grade
const marks = [80, 86, 99];
console.log(calculateGrade(marks));
function calculateGrade(marks) {
const average = calculateAverage(marks);
if (average < 60)
return 'F';
else if (average < 70)
return 'D';
else if (average < 80)
return 'C';
else if (average < 90)
return 'B';
return 'A';
}
function calculateAverage(array) {
let sum = 0;
for (let value of array) {
sum += value;
}
return sum / array.length;
}
Exercise-10 Stars
ShowStars(6);
function showStars(rows) {
for (let row = 1; row <= rows; row++) {
let pattern = '';
for (let i = 0; i < row; i++) {
pattern += '*';
}
console.log(pattern);
}
}
Exercise-11 Prime Numbers
Prime (whose factors are only 1 and itself)
Composite
/*
showPrimes(20);
function showPrimes(limit) {
for (let number = 2; number <= limit; number++) {
let isPrime = true;
for (let factor = 2; factor < number; factor++) {
if (number % factor === 0) {
isPrime = false;
break;
}
}
if (isPrime)
console.log(number);
}
}
*/
showPrimes(20);
function showPrimes(limit) {
for (let number = 2; number <= limit; number++) {
if (isPrime(number))
console.log(number);
}
}
function isPrime(number) {
for (let factor = 2; factor < number; factor++) {
if (number % factor === 0)
return false;
}
return true;
}