Code / JavaScript from Zero / Unit 2: Strings and decisions

Making decisions फ़ैसले लेना

Lesson 4 of 15 · about 14 minutes · if / else if / else, comparisons, && and ||.

⚡ 0 XP

Step 1 of 2

if, else if, else

if ?true ✓else ✗

Programs make choices with if. JavaScript checks each condition from top to bottom and runs the first block whose condition is true:

  • if (marks >= 90) { ... }
  • else if (marks >= 75) { ... }
  • else { ... } runs when nothing above was true

Comparison operators: > < >= <=, === (equal) and !== (not equal). Always use three equals === to compare.

In this challenge your code sits inside a small function gradeFor(marks) so we can test it with many marks. return sends the answer back. You will learn functions fully in Unit 4.

Example
const temp = 41;
if (temp >= 40) {
  console.log("Very hot! Drink nimbu paani.");
} else if (temp >= 30) {
  console.log("Warm day.");
} else {
  console.log("Pleasant weather.");
}

🎯 Challenge: Grades from marks +15 XP

Complete gradeFor(marks) so it returns:

  • "A" for 90 or more
  • "B" for 75 to 89
  • "C" for 50 to 74
  • "D" below 50

Use if, else if and else.

Try twice to unlock the solution (0/2)

script.jsCtrl ⏎
Console

Output from console.log appears here.

Checklist

Press Run to check your code.

  • 95 and 90 get an A
  • 89 and 75 get a B
  • 74 and 50 get a C
  • 49 and 0 get a D
  • else if is used
Lesson notes (text version)

1. if, else if, else

Programs make choices with if. JavaScript checks each condition from top to bottom and runs the first block whose condition is true:

  • if (marks >= 90) { ... }
  • else if (marks >= 75) { ... }
  • else { ... } runs when nothing above was true

Comparison operators: > < >= <=, === (equal) and !== (not equal). Always use three equals === to compare.

In this challenge your code sits inside a small function gradeFor(marks) so we can test it with many marks. return sends the answer back. You will learn functions fully in Unit 4.

                const temp = 41;
if (temp >= 40) {
  console.log("Very hot! Drink nimbu paani.");
} else if (temp >= 30) {
  console.log("Warm day.");
} else {
  console.log("Pleasant weather.");
}
              

Challenge: Grades from marks

Complete gradeFor(marks) so it returns:

  • "A" for 90 or more
  • "B" for 75 to 89
  • "C" for 50 to 74
  • "D" below 50

Use if, else if and else.

2. AND, OR and NOT

Combine conditions with logical operators:

  • && (AND): true only if both sides are true — age >= 18 && hasVoterId
  • || (OR): true if at least one side is true — age < 12 || age >= 60
  • ! (NOT): flips true and false — !isRaining

Cinema ticket example: children under 12 or seniors 60+ get a discount.

                const age = 65;
const hasTicket = true;
if ((age < 12 || age >= 60) && hasTicket) {
  console.log("Discount seat!");
}
              

Challenge: Cinema tickets and voting

Complete two functions.

ticketPrice(age, isStudent) returns:

  • 0 if age is under 3 (free)
  • 60 if age is under 12 or 60 and above
  • 80 for a student (any other age)
  • 120 for everyone else

canVote(age, hasVoterId) returns true only if age is 18 or more and they have a voter ID.

Search by task, format or tool name.