Created
September 13, 2025 17:18
-
-
Save davidystephenson/0c5b2b7e02d146dd4a07b54c353053bc 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 = 0 | |
| var positive = num > 0 | |
| var negative = num < 0 | |
| if (positive) { | |
| console.log('positive') | |
| } else if (negative) { | |
| console.log('negative') | |
| } else { | |
| console.log('zero') | |
| } | |
| // 2. Write a program that checks if a given number, `num`, is even or odd. | |
| var num = 5 | |
| 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 = 2020 | |
| var fourRemainer = year % 4 | |
| var divisibleBy4 = fourRemainer === 0 | |
| var hundredRemainder = year % 100 | |
| var notDivisibleBy100 = hundredRemainder !== 0 | |
| var fourHundredRemainder = year % 400 | |
| var divisibleBy400 = fourHundredRemainder === 0 | |
| var normalLeap = divisibleBy4 && notDivisibleBy100 | |
| var leap = normalLeap || 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 = 30 | |
| var under12 = age < 12 | |
| var over65 = age > 64 | |
| 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 = 30 | |
| 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