Created
April 5, 2025 17:12
-
-
Save davidystephenson/ab0642bbcf5777787f8a545cf5bf8557 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Write a program that checks if a given number, `num`, is positive, negative, or zero. | |
var num = 3 | |
if (num > 0) { | |
console.log('positive') | |
} else if (num < 0) { | |
console.log('negative') | |
} else { | |
console.log('zero') | |
} | |
// 2. Write a program that checks if a given number, `num`, is even or odd. | |
var num = 4 | |
var remainder = num % 2 | |
var even = remainder == 0 | |
if (even) { | |
console.log('even') | |
} else { | |
console.log('odd') | |
} | |
// 3. Write a program that determines if a given year, `year`, is a leap year and | |
// has 366 days, or a common year and has 365 days. | |
/* | |
We use an if statement to check if the year is a leap year based on the following conditions: | |
- If the year is divisible by 4 (year % 4 === 0). | |
- But not divisible by 100 (year % 100 !== 0). | |
- Or it is divisible by 400 (year % 400 === 0). | |
If any of these conditions are true, the year is considered a leap year and has 366 days. Otherwise, it is a common year and has 365 days. | |
*/ | |
var year = 2000 | |
var divisibleByFour = year % 4 == 0 | |
var divisibleBy100 = year % 100 == 0 | |
var divisibleBy400 = year % 400 == 0 | |
var leap = (divisibleByFour && !divisibleBy100) || divisibleBy400 | |
if (leap) { | |
console.log(366) | |
} else { | |
console.log(365) | |
} | |
// 4. Develop a program that determines the price of a movie ticket based on a person's age. If the person is under 12 or over 65, the ticket price is $7; otherwise, it's $12. | |
var age = 34 | |
var under12 = age < 12 | |
var over65 = age > 65 | |
var discount = under12 || over65 | |
if (discount) { | |
console.log(7) | |
} else { | |
console.log(12) | |
} | |
// 5. Write a program that categorizes a person's age into different groups: child (0-12 years), teenager (13-19 years), adult (20-64 years), and senior (65+ years). | |
var age = 34 | |
if (age < 13) { | |
console.log('child') | |
} else if (age < 20) { | |
console.log('teenager') | |
} else if (age < 65) { | |
console.log('adult') | |
} else { | |
console.log('senior') | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment