4. Template Literal & IF /ELSE Coding Challenge Flashcards

1
Q

Recreate the ‘description’ variable from the last assignment, this time
using the template literal syntax

” India is in Asia and its 135 crores people speaks in Hindi.”
TIP :- Define Country, Continent and Language.

A
const description = `${country} is in ${continent}, and its
${population} million people speak ${language}`;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

If your country’s population is greater that 33 million, log a string like this to the console: ‘Portugal’s population is above average’. Otherwise, log a string like
‘Portugal’s population is 22 million below average’ (the 22 is the average of 33 minus the country’s population)
2. After checking the result, change the population temporarily to 13 and then to
130. See the different results, and set the population back to original

A
if (population > 33) {
console.log(`${country}'s population is above average`);
} else {
console.log(
`${country}'s population is ${33 - population} million
below average`,
);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. Check if a number is odd or even in JavaScript
    Function isEvenOrOdd() checks if input number is even or odd by using “%” operator in JavaScript.

Print “Number is even” if the number is divisible by 2.
Else print “Number is odd” if the number returns a remainder when divided by 2.

A

function isEvenorOdd(num) {

if(num % 2 == 0){
console.log(${num} is a Even number)
}
else{
console.log(${num} is a Odd number)
}
}

isEvenorOdd(10) //”10 is a Even number”
isEvenorOdd(19) //”19 is a Odd number”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Check if input variable is a number or not
Function isNumber() checks if input variable is a number by using isNaN() in-built JavaScript function. Read more about isNan() from w3schools.com/jsref/jsref_isnan.asp.

Print “Variable is not a number” if isNaN() returns true.
Else print “Variable is a valid number” if isNaN() returns false.

A

function isValidNumber(num) {

if(isNaN(num)){
console.log(${num} is not a number)
}
else{
console.log(${num} is a valid number)
}
}

isValidNumber(11) //”11 is a valid number”
isValidNumber(“19”) //”19 is a valid number”
isValidNumber(“xyz”) //”xyz is not a number”
isValidNumber(“17.5”) //”17.5 is a valid number”
isValidNumber(“21F”) //”21F is not a number”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Function findLargest() finds the largest between two number by using “>” and “=” operator in JavaScript.

Print num1 is the largest if num1>num2.
Print num2 is the largest if num1

A

function findLargest(num1, num2) {

  if(num1 > num2){
    console.log(`${num1} is the largest number`)
  }
  else if (num2 > num1){
    console.log(`${num2} the largest number`)
  }
  else{
    console.log(`${num1} is equal to ${num2}`)
  }
}

findLargest(21,45) //”45 the largest number”
findLargest(34,18) //”34 is the largest number”
findLargest(41,41) //”41 is equal to 41”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Function findLargest() finds the largest of three number by using “>” and “&&” operator in JavaScript.

Print num1 is the largest if num1>num2 and num1>num3.
Print num2 is the largest if num2

A

function findLargest(num1, num2, num3) {

  if(num1 > num2 && num1 > num3){
    console.log(`${num1} is the largest number`)
  }
  else if (num2 > num3){
    console.log(`${num2} is the largest number`)
  }
  else{
    console.log(`${num3} is the largest number`)
  }
}

findLargest(21,45,13) //”45 is the largest number”
findLargest(34,18,52) //”52 is the largest number”
findLargest(64,11,11) //”64 is the largest number”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Check if a triangle is equilateral, scalene, or isosceles
Function findTriangleType() finds the type of the triangle for given side values by using “==” and “&&” operator in JavaScript.

Print “Equilateral triangle.” if values for all side1, side2 and side3 are equal.
Print “Isosceles triangle.” if values for side1 is equal to side2 or side2 is equal to side3
Else “Scalene triangle.” since values of all sides are unequal.

A

function findTriangleType(side1, side2, side3) {

  if((side1 == side2) && (side1 == side3)){
    console.log(`Equilateral triangle.`)
  }
  else if ((side1 == side2) || (side2 == side3)){
    console.log(`Isosceles triangle.`)
  }
  else{
    console.log(`Scalene triangle.`)
  }
}

findTriangleType(12,12,12) //”Equilateral triangle.”
findTriangleType(20,20,31) //”Isosceles triangle.”
findTriangleType(5,4,3) //”Scalene triangle.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Function checkInRange() finds if the given number is within the provided start and end range using >=, <= and && operators in JavaScript.

Print “Between the range” if num is between start and end values
Else Print “Outside the range” since num is outside start and end values.

A

function checkInRange(num, start, end) {

if(num >= start && num <= end){
console.log(${num} is between the range ${start} and ${end})
}
else{
console.log(${num} is outside the range ${start} and ${end})
}
}

checkInRange(15,11,30) //”15 is between the range 11 and 30”
checkInRange(20,34,51) //”20 is outside the range 34 and 51”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Perform arithmetic operations on two numbers
Function evalNumbers() prints the result after evaluating arithmetic operations between two numbers of addition, multiplication, division, and modulus in JavaScript.

Print result of num1+num2 if operation is “add”
Print result of num1-num2 if operation is “subtract”
Print result of num1*num2 if operation is “multiply”
Print result of num1/num2 if operation is “divide”
Print result of num1%num2 if operation is “modulus”
Else print “Invalid operation”

A

function evalNumbers(num1, num2, op) {

  if(op == "add"){
    console.log(`Sum of ${num1} and ${num2} is ${num1+num2}`)
  }
  else if(op == "subtract"){
    console.log(`Difference of ${num1} and ${num2} is ${num1-num2}`)
  }
  else if(op == "multiply"){
    console.log(`Product of ${num1} and ${num2} is ${num1*num2}`)
  }
  else if(op == "divide"){
    console.log(`Division of ${num1} and ${num2} is ${num1/num2}`)
  }
  else if(op == "modulus"){
    console.log(`Modulus of ${num1} and ${num2} is ${num1%num2}`)
  }
  else{
     console.log(`${op} is an invalid operation`)    
  }
}

evalNumbers(15,10,”add”) //”Sum of 15 and 10 is 25”
evalNumbers(20,8,”subtract”) //”Difference of 20 and 8 is 12”
evalNumbers(12,4,”multiply”) //”Product of 12 and 4 is 48”
evalNumbers(28,7,”divide”) //”Division of 28 and 7 is 4”
evalNumbers(22,3,”modulus”) //”Modulus of 22 and 3 is 1”
evalNumbers(31,3,”square”) //”square is an invalid operation”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Find check if a year is leap year or not
Function checkLeapYear() find if the given year is a leap year or not by using %, !=, && and || operators in JavaScript.

If year is divisble by 4 and not divisble by 100 then print “leap year”.
Or if year is divisible by 400 then print “leap year”.
Else print “not a leap year”.

A

function checkLeapYear(year) {

if(((year%4 == 0)&&(year%100 != 0))||(year%400 == 0)){
console.log(Year ${year} is a leap year);
}
else{
console.log(Year ${year} is not a leap year);
}
}

checkLeapYear(2012) //”Year 2012 is a leap year”
checkLeapYear(1900) //”Year 1900 is not a leap year”
checkLeapYear(2000) //”Year 2000 is a leap year”
checkLeapYear(2011) //”Year 2011 is not a leap year”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Find the grade for input marks
Function findGrade() to find the grade of the student based on the input marks.

Print “S grade” if marks is between 90 and 100.
Print “A grade” if marks is between 80 and 90.
Print “B grade” if marks is between 70 and 80.
Print “C grade” if marks is between 60 and 70.
Print “D grade” if marks is between 50 and 60.
Print “E grade” if marks is between 40 and 50.
Print “Student has failed” if marks is between 0 and 40.
Else print “Invalid marks”.

A

function findGrade(name, marks) {

  if(marks >= 90 && marks <= 100){
    console.log(`${name} you have received S grade`)
  }
  else if(marks >= 80 && marks < 90){
    console.log(`${name} you have received A grade`)
  }
  else if(marks >= 70 && marks < 80){
    console.log(`${name} you have received B grade`)
  }
  else if(marks >= 60 && marks < 70){
    console.log(`${name} you have received C grade`)
  }
  else if(marks >= 50 && marks < 60){
    console.log(`${name} you have received D grade`)
  }
  else if(marks >= 40 && marks < 50){
    console.log(`${name} you have received E grade`)
  }
  else if(marks >= 0 && marks <40){
    console.log(`${name} you have Failed`)
  }
  else{
    console.log(`Invalid marks of ${marks}`)
  }
}

findGrade(“John Doe”, 91) //”John Doe you have received S grade”
findGrade(“John Doe”, 85) //”John Doe you have received A grade”
findGrade(“John Doe”, 73) //”John Doe you have received B grade”
findGrade(“John Doe”, 53) //”John Doe you have received D grade”
findGrade(“John Doe”, 20) //”John Doe you have Failed”
findGrade(“John Doe”, 120) //”Invalid marks of 120”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Find number of days in a given month
Function findDaysInMonth() finds the number of days in a given month of a year.

If month is outside the range of 1 and 12 print “Invalid month”.
If month is equal to 2 ie, February print “29 days” if leap year else print “28 days” .
Else if month is equal to 4, 6, 9 or 11 print “30 days”.
Else print “31 days”.

A

function isLeapYear(year){

return (((year%4 == 0)&&(year%100 != 0))||(year%400 == 0));
}

function findDaysInMonth(month, year) {

if(month < 1 || month > 12){
console.log(Invalid Month of value ${month})
return;
}

if(month ==2){
if(isLeapYear(year)){
console.log(The Month has 29 days)
}
else{
console.log(The Month has 28 days)
}
}
else if(month == 4 || month == 6 || month == 9 || month== 11){
console.log(The Month has 30 days)
}
else{
console.log(The Month has 31 days)
}
}

findDaysInMonth(2, 2012) //”The Month has 29 days”
findDaysInMonth(2, 2013) //”The Month has 28 days”
findDaysInMonth(4, 2012) //”The Month has 30 days’
findDaysInMonth(10, 2013) //”The Month has 31 days”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly