Assignments of Day 4: Operators in JavaScript
4o mini
You said:
Give some assignments with solutions from same concept above
ChatGPT said:
ChatGPT
Here are some assignments based on Operators in JavaScript, along with their solutions.
Assignment 1: Arithmetic Operators
Task:
1. Create a JavaScript program that performs the following:
o Add two numbers and store the result in a variable.
o Subtract one number from another and store the result in a variable.
o Multiply two numbers and store the result in a variable.
o Find the modulus (remainder) of a division and store the result in a variable.
o Divide one number by another and store the result in a variable.
Solution:
javascriptCopy codelet num1 = 10;let num2 = 5; // Additionlet sum = num1 + num2;console.log("Sum:", sum); // Output: 15 // Subtractionlet difference = num1 - num2;console.log("Difference:", difference); // Output: 5 // Multiplicationlet product = num1 * num2;console.log("Product:", product); // Output: 50 // Moduluslet remainder = num1 % num2;console.log("Remainder:", remainder); // Output: 0 // Divisionlet quotient = num1 / num2;console.log("Quotient:", quotient); // Output: 2Assignment 2: Comparison Operators
Task:
1. Write a program that compares two numbers using different comparison operators:
o Equal to (==)
o Strict equal to (===)
o Not equal to (!=)
o Greater than (>)
o Less than (<)
o Greater than or equal to (>=)
o Less than or equal to (<=)
2. Print out the results of each comparison.
Solution:
javascriptCopy codelet num1 = 10;let num2 = 5; console.log("num1 == num2:", num1 == num2); // falseconsole.log("num1 === num2:", num1 === num2); // false (compares both value and type)console.log("num1 != num2:", num1 != num2); // trueconsole.log("num1 > num2:", num1 > num2); // trueconsole.log("num1 < num2:", num1 < num2); // falseconsole.log("num1 >= num2:", num1 >= num2); // trueconsole.log("num1 <= num2:", num1 <= num2); // falseAssignment 3: Logical Operators
Task:
1. Write a program that checks if two conditions are true using:
o Logical AND (&&)
o Logical OR (||)
o Logical NOT (!)
2. Test the program with different values and print the result for each operator.
Solution:
javascriptCopy codelet condition1 = true;let condition2 = false; // Logical ANDlet resultAnd = condition1 && condition2;console.log("condition1 && condition2:", resultAnd); // false, because both conditions must be true // Logical ORlet resultOr = condition1 || condition2;console.log("condition1 || condition2:", resultOr); // true, because only one condition needs to be true // Logical NOTlet resultNot = !condition1;console.log("!condition1:", resultNot); // false, because condition1 is true, so NOT makes it falseAssignment 4: Assignment Operators
Task:
1. Write a program that initializes a variable and then updates it using the assignment operators:
o += (Add and assign)
o -= (Subtract and assign)
o *= (Multiply and assign)
o /= (Divide and assign)
2. Print the value of the variable after each update.
Solution:
javascriptCopy codelet value = 10; value += 5; // value = value + 5console.log("After += 5, value:", value); // Output: 15 value -= 3; // value = value - 3console.log("After -= 3, value:", value); // Output: 12 value *= 2; // value = value * 2console.log("After *= 2, value:", value); // Output: 24 value /= 4; // value = value / 4console.log("After /= 4, value:", value); // Output: 6Assignment 5: Mixed Operators
Task:
1. Write a program that performs the following:
o Add two numbers and store the result.
o Multiply the result from the previous step by another number.
o Subtract a value from the product obtained.
o Find the modulus of the final result with a divisor.
2. Print the final result.
Solution:
javascriptCopy codelet num1 = 5;let num2 = 3;let num3 = 2; // Step 1: Add two numberslet result = num1 + num2; // 5 + 3 = 8console.log("After addition:", result); // Step 2: Multiply the result by another numberresult *= num3; // 8 * 2 = 16console.log("After multiplication:", result); // Step 3: Subtract a value from the resultresult -= 4; // 16 - 4 = 12console.log("After subtraction:", result); // Step 4: Find the modulusresult = result % 5; // 12 % 5 = 2 (remainder when 12 is divided by 5)console.log("Final result (modulus):", result); // Output: 2Assignment 6: Check Even or Odd
Task:
1. Write a JavaScript program that:
o Accepts a number.
o Uses the modulus operator (%) to determine if the number is even or odd.
2. If the number is divisible by 2 (i.e., remainder is 0), it's even; otherwise, it's odd.
Solution:
javascriptCopy codelet number = 7; // Check if the number is even or oddlet result = (number % 2 === 0) ? "Even" : "Odd";console.log("The number", number, "is", result); // Output: The number 7 is OddAssignment 7: Nested Logical Conditions
Task:
1. Write a program that checks the following conditions:
o If a number is greater than 10 and less than 20, print "Between 10 and 20".
o Otherwise, if the number is less than 10, print "Less than 10".
o Otherwise, print "Greater than or equal to 20".
Solution:
javascriptCopy codelet number = 15; // Check the range of the numberif (number > 10 && number < 20) { console.log("Between 10 and 20");} else if (number < 10) { console.log("Less than 10");} else { console.log("Greater than or equal to 20");}Assignment 8: Using All Operators
Task:
1. Write a program that:
o Initializes two variables.
o Adds them together.
o Subtracts a value from the sum.
o Divides the result by another number.
o Compares the result with a fixed value to check if it's greater than or equal.
o Displays the result using both comparison and logical operators.
Solution:
javascriptCopy codelet a = 10;let b = 5;let c = 3; let result = a + b; // Add a and bresult -= c; // Subtract c from resultresult /= 2; // Divide the result by 2 // Check if the result is greater than or equal to 5let isGreaterOrEqual = result >= 5; // Use logical AND to combine comparison and other conditionif (isGreaterOrEqual && result < 10) { console.log("Result is greater than or equal to 5, but less than 10.");} else { console.log("Result does not meet the condition.");} console.log("Final result:", result); // Output the final resultThese assignments and their solutions provide a comprehensive understanding of operators in JavaScript and help reinforce the concepts learned in the lecture. Feel free to modify the assignments and experiment with different values to deepen your understanding.
