Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidystephenson/57c9e9179d28882031e3257782bf3db3 to your computer and use it in GitHub Desktop.
Save davidystephenson/57c9e9179d28882031e3257782bf3db3 to your computer and use it in GitHub Desktop.
// 1. Write a JavaScript expression to calculate the sum of two numbers, `num1` and `num2`.
var num1 = 5
var num2 = 1
var sum = num1 + num2
console.log(sum)
// 2. Write a program to calculate the area of a rectangle given its length and width.
var length = 3
var width = 9
var area = length * width
console.log(area)
// 3. Write a program to calculate the remainder when a given number, `dividend`, is divided by another number, `divisor`.
var dividend = 100
var divisor = 3
var remainder = 100 % 3
console.log(remainder)
// 4. Write a program to increment a variable, `count`, by 1.
var count = 7
count++
console.log(count)
// 5. Write a program to concatenate two strings, `str1` and `str2`, using the concatenation operator (+).
var str1 = 'hello'
var str2 = 'world'
var str3 = str1 + str2
console.log(str3)
// 6. Write a program to negate a boolean value, `isTrue`.
var isTrue = true
var negated = !isTrue
console.log(negated)
// 7. Write a program to check if a given number, `num`, is between 10 and 20 (inclusive).
var num = 15
var atLeastTen = num >= 10
var twentyOrLess = num <= 20
var between = atLeastTen && twentyOrLess
console.log(between)
// 8. Write a program to check if a person is eligible for a discount based on the age (`age`) and membership status (`isMember`). The discount is applicable if the person is at least 60 years old or is a member.
var age = 75
var isMember = false
var atLeastSixty = age >= 60
var discount = atLeastSixty || isMember
console.log(discount)
// 9. Write a program to check if a given number, `num`, is not equal to 0.
var num = 5
var notEqual = num != 0
console.log(notEqual)
// 10. Write a program to check if a student is eligible for a scholarship based on their grade (`grade`) and attendance (`attendance`). The student is eligible if their grade is at least 90 or their attendance is at least 95%.
var grade = 80
var attendance = 0.99
var atLeast90 = grade >= 90
var atLeast95Percent = attendance >= 0.95
var scholarship = atLeast90 || atLeast95Percent
console.log(scholarship)
// 11. Write a program to check if a given string, `str`, is either "apple" or "banana".
var str = 'orange'
var apple = str == 'apple'
var banana = str == 'banana'
var appleOrBanana = apple || banana
console.log(appleOrBanana)
// 12. Write a program to check if a given number, `num`, is both positive and even.
var num = 5
var positive = num > 0
var remainder = num % 2
var even = remainder == 0
var positiveAndEven = positive && even
console.log(positiveAndEven)
// 13. Write a program to check if a person is eligible to vote based on their age (`age`) and citizenship (`isCitizen`). The person is eligible if they are at least 18 years old and a citizen.
var age = 21
var isCitizen = true
var vote = age >= 18 && isCitizen
console.log(vote)
// 14. Write a program to check if a given number `num` is out of range of 0 and 100, is either less than 0 or greater than 100.
var num = 50
var lessThan0 = num < 0
var greaterThan100 = num > 100
var outOfRange = lessThan0 || greaterThan100
console.log(outOfRange)
// 15. Write a program to swap 2 numbers
var x = 1
var y = 2
var z = x
x = y
y = z
console.log(x)
console.log(y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment