Assignments of Day 4: Operators in JavaScript

Rashmi Mishra
0

 

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:

javascript
Copy code
let num1 = 10;
let num2 = 5;
 
// Addition
let sum = num1 + num2;
console.log("Sum:", sum);  // Output: 15
 
// Subtraction
let difference = num1 - num2;
console.log("Difference:", difference);  // Output: 5
 
// Multiplication
let product = num1 * num2;
console.log("Product:", product);  // Output: 50
 
// Modulus
let remainder = num1 % num2;
console.log("Remainder:", remainder);  // Output: 0
 
// Division
let quotient = num1 / num2;
console.log("Quotient:", quotient);  // Output: 2

Assignment 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:

javascript
Copy code
let num1 = 10;
let num2 = 5;
 
console.log("num1 == num2:", num1 == num2);  // false
console.log("num1 === num2:", num1 === num2);  // false (compares both value and type)
console.log("num1 != num2:", num1 != num2);  // true
console.log("num1 > num2:", num1 > num2);  // true
console.log("num1 < num2:", num1 < num2);  // false
console.log("num1 >= num2:", num1 >= num2);  // true
console.log("num1 <= num2:", num1 <= num2);  // false

Assignment 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:

javascript
Copy code
let condition1 = true;
let condition2 = false;
 
// Logical AND
let resultAnd = condition1 && condition2;
console.log("condition1 && condition2:", resultAnd);  // false, because both conditions must be true
 
// Logical OR
let resultOr = condition1 || condition2;
console.log("condition1 || condition2:", resultOr);  // true, because only one condition needs to be true
 
// Logical NOT
let resultNot = !condition1;
console.log("!condition1:", resultNot);  // false, because condition1 is true, so NOT makes it false

Assignment 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:

javascript
Copy code
let value = 10;
 
value += 5;  // value = value + 5
console.log("After += 5, value:", value);  // Output: 15
 
value -= 3;  // value = value - 3
console.log("After -= 3, value:", value);  // Output: 12
 
value *= 2;  // value = value * 2
console.log("After *= 2, value:", value);  // Output: 24
 
value /= 4;  // value = value / 4
console.log("After /= 4, value:", value);  // Output: 6

Assignment 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:

javascript
Copy code
let num1 = 5;
let num2 = 3;
let num3 = 2;
 
// Step 1: Add two numbers
let result = num1 + num2;  // 5 + 3 = 8
console.log("After addition:", result);
 
// Step 2: Multiply the result by another number
result *= num3;  // 8 * 2 = 16
console.log("After multiplication:", result);
 
// Step 3: Subtract a value from the result
result -= 4;  // 16 - 4 = 12
console.log("After subtraction:", result);
 
// Step 4: Find the modulus
result = result % 5;  // 12 % 5 = 2 (remainder when 12 is divided by 5)
console.log("Final result (modulus):", result);  // Output: 2

Assignment 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:

javascript
Copy code
let number = 7;
 
// Check if the number is even or odd
let result = (number % 2 === 0) ? "Even" : "Odd";
console.log("The number", number, "is", result);  // Output: The number 7 is Odd

Assignment 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:

javascript
Copy code
let number = 15;
 
// Check the range of the number
if (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:

javascript
Copy code
let a = 10;
let b = 5;
let c = 3;
 
let result = a + b;  // Add a and b
result -= c;  // Subtract c from result
result /= 2;  // Divide the result by 2
 
// Check if the result is greater than or equal to 5
let isGreaterOrEqual = result >= 5;
 
// Use logical AND to combine comparison and other condition
if (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 result

These 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.


Post a Comment

0Comments

Post a Comment (0)