Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions arthimetic-operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,20 @@ Task 1:
7. Use console.log() to print the value of variable e to the console. (it should result 102.000)
*******************************************************************************/
// TODO: ADD YOUR CODE BELOW
let a = (4 + 6)
console.log(a)

let b = (10 * 5)
console.log(b)

let c = (17 % 3)
console.log(c)

let d = (b - a)
console.log(d)

let e = (a + b + c + d)
console.log(e)
/*******************************************************************************
Task 2:
1. Subtract two numbers and log the result to the console.
Expand All @@ -28,3 +41,19 @@ Task 1:
5. Divide two numbers and round the result to the nearest integer before logging it to the console.
*******************************************************************************/
// TODO: ADD YOUR CODE BELOW
let subtraction = (10 - 10)
console.log(subtraction)

let multiplication = (3 * 1 * 5 * 7)
console.log(multiplication)

let volumeOfRectangularPrism = (3 * 5 * 7)
console.log(volumeOfRectangularPrism)

let price = 9.99
let discount = 0.20
let discountedPrice = (price - discount)
console.log(discountedPrice.toFixed(3))

let division = (120/20)
console.log(division.toFixed(3))
18 changes: 10 additions & 8 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Variables and Operators</title>
</head>
<body>
<script src="./variables.js"></script>
</head>
<body>
<!-- <script src="./variables.js"></script> -->
<!-- <script src="./arthimetic-operators.js"></script> -->
<script src="./logical-comaprison-operators.js"></script>
<!-- <script src="./arthimetic-operators.js"></script> -->
<!-- <script src="./logical-comaprison-operators.js"></script> -->
</body>
</body>
</html>
51 changes: 26 additions & 25 deletions logical-comaprison-operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,35 @@ TASK 1:
// Have fun! // 😃
********************************************************************************/

const exp1 = 10 >= 10; // TODO: ADD YOUR EVALUATION HERE -->
const exp1 = 10 >= 10; // TODO: ADD YOUR EVALUATION HERE --> true

const exp2 = "dog" == "dog"; // TODO: ADD YOUR EVALUATION HERE -->
const exp2 = "dog" == "dog"; // TODO: ADD YOUR EVALUATION HERE --> true

const exp3 = true != false; // TODO: ADD YOUR EVALUATION HERE -->
const exp3 = true != false; // TODO: ADD YOUR EVALUATION HERE --> false
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should return true instead


const exp4 = "10" === 10; // TODO: ADD YOUR EVALUATION HERE -->
const exp4 = "10" === 10; // TODO: ADD YOUR EVALUATION HERE --> false

const exp5 = 5 > 4; // TODO: ADD YOUR EVALUATION HERE -->
const exp5 = 5 > 4; // TODO: ADD YOUR EVALUATION HERE --> true

const exp6 = null == undefined; // TODO: ADD YOUR EVALUATION HERE -->
const exp6 = null == undefined; // TODO: ADD YOUR EVALUATION HERE --> false
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should return true, because both of these values are falsey values by default, meaning that false == false.

This would return false only when using the strict equals operator === which would also check for the types resulting in them being false.


const exp7 = "true" == true; // TODO: ADD YOUR EVALUATION HERE -->
const exp7 = "true" == true; // TODO: ADD YOUR EVALUATION HERE --> false

const exp8 = "false" == false; // TODO: ADD YOUR EVALUATION HERE -->
const exp8 = "false" == false; // TODO: ADD YOUR EVALUATION HERE --> false

const exp9 = NaN === NaN; // TODO: ADD YOUR EVALUATION HERE -->
const exp9 = NaN === NaN; // TODO: ADD YOUR EVALUATION HERE --> true
Copy link
Copy Markdown

@AlhassanAli01 AlhassanAli01 Feb 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should evaluate to false.
The reason NaN === NaN evaluates to false is because according to the IEEE 754 standard (which JavaScript follows for floating-point arithmetic), NaN values are considered unordered. This means that they are not equal to any other value, including other NaN values.


const exp10 = !false || false; // TODO: ADD YOUR EVALUATION HERE -->
const exp10 = !false || false; // TODO: ADD YOUR EVALUATION HERE --> true

const exp11 = false && !false; // TODO: ADD YOUR EVALUATION HERE -->
const exp11 = false && !false; // TODO: ADD YOUR EVALUATION HERE --> false

const exp12 = "apple" > "pineapple"; // TODO: ADD YOUR EVALUATION HERE -->
const exp12 = "apple" > "pineapple"; // TODO: ADD YOUR EVALUATION HERE --> apple
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the expression "apple" > "pineapple", JavaScript compares the strings character by character from left to right. Since "a" comes before "p" in the alphabet, "apple" is considered "less than" "pineapple". Therefore, "apple" > "pineapple" evaluates to false.


const exp13 = "2" > "12"; // TODO: ADD YOUR EVALUATION HERE -->
const exp13 = "2" > "12"; // TODO: ADD YOUR EVALUATION HERE --> false
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should evaluate to true.
Even though numerically 2 is less than 12, since we're comparing strings, the comparison is based on their lexicographical order. In this case, the string "2" is considered greater than the string "12" because "2" comes after "1" lexicographically.


const exp14 = undefined == null; // TODO: ADD YOUR EVALUATION HERE -->
const exp14 = undefined == null; // TODO: ADD YOUR EVALUATION HERE --> false
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should evaluate to true.
Both null & undefined are falsey values, and because we're using == we're basically saying false == false which should evaluate to true.
If we were using === it should indeed be false since we're comparing values and types as well.


const exp15 = undefined === null; // TODO: ADD YOUR EVALUATION HERE -->
const exp15 = undefined === null; // TODO: ADD YOUR EVALUATION HERE --> false

/*******************************************************************************
Task 2:
Expand All @@ -47,33 +47,34 @@ const isHappy = false;

// - Check if num is between 10 and 20 (inclusive) using the logical AND operator. Log the result to the console.
// TODO: ADD YOUR CODE BELOW

console.log(num >= 10 && num <= 20);
// - Check if num is either less than 5 or greater than 50 using the logical OR operator. Log the result to the console.
// TODO: ADD YOUR CODE BELOW

console.log(num < 5 || num >50)
// - Check if str is either "apple" or "orange" using the logical OR operator. Log the result to the console.
// TODO: ADD YOUR CODE BELOW

console.log(str == "apple" || str == "orange")
// - Check if isHappy value is true using the logical NOT operator. Log the result to the console.
// TODO: ADD YOUR CODE BELOW

console.log(!isHappy)
// - Check if num is even and greater than 10 using the logical AND operator. Log the result to the console.
// TODO: ADD YOUR CODE BELOW

console.log(num == 10 && num >10)
// - Check if num is divisible by both 3 and 5 using the logical OR operator. Log the result to the console.
// TODO: ADD YOUR CODE BELOW

console.log(num/3 || num/5)
// - Check if str contains the letter "e". Log the result to the console.
// TODO: ADD YOUR CODE BELOW

console.log(str.includes("e"))
// - Check if str starts with "Hakuna". Log the result to the console.
// TODO: ADD YOUR CODE BELOW

console.log(str.startsWith("Hakuna"))
// - Check if str ends with "a". Log the result to the console.
// TODO: ADD YOUR CODE BELOW

console.log(str.endsWith("a"))
// - Check if num is either negative or odd using the logical OR operator. Log the result to the console.
// TODO: ADD YOUR CODE BELOW

console.log(num == "negative"|| num == "odd")
Copy link
Copy Markdown

@AlhassanAli01 AlhassanAli01 Feb 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To check if the number is negative correctly:
num < 0
To check if the number is odd we use the mod operator:
num % 2 !== 0
or
num % 2 === 1

// - Check if the length of str is greater than num or equal to 40 using logical OR operator. Log the result to the console.
// TODO: ADD YOUR CODE BELOW
console.log(str.length > num || str.length == 40)