Skip to content

Commit e8764ad

Browse files
committed
type conversion note and example code
1 parent 5de193f commit e8764ad

66 files changed

Lines changed: 1818 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.history/.gitignore_20250701032400

46 Bytes
Binary file not shown.

.history/.gitignore_20250701032515

30 Bytes
Binary file not shown.

.history/.gitignore_20250701032516

28 Bytes
Binary file not shown.

.history/.gitignore_20250701032549

82 Bytes
Binary file not shown.

.history/.gitignore_20250701032551

28 Bytes
Binary file not shown.

.history/pracrice/Arithmetic Operators_20250701042205.js

Whitespace-only changes.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// 🌟 Arithmetic Operators in JavaScript
2+
3+
// 1. Basic Arithmetic Operations
4+
const x = 12;
5+
const y = 3;
6+
7+
console.log("=== Basic Arithmetic Operations ===");
8+
console.log("Addition:", x + y); // 15
9+
console.log("Subtraction:", x - y); // 9
10+
console.log("Multiplication:", x * y); // 36
11+
console.log("Division:", x / y); // 4
12+
console.log("Remainder:", x % y); // 0
13+
console.log("Exponentiation x ** 2:", x ** 2); // 144
14+
console.log("Exponentiation y ** 3:", y ** 3); // 27
15+
16+
// 2. String Addition
17+
const a = "12";
18+
const b = "3";
19+
20+
console.log("\n=== String Addition (Concatenation) ===");
21+
console.log("a + b:", a + b); // "123" (string concatenation)
22+
23+
// 3. Other Operations with String Numbers
24+
console.log("\n=== Arithmetic with String Numbers ===");
25+
console.log("a - b:", a - b); // 9
26+
console.log("a * b:", a * b); // 36
27+
console.log("a / b:", a / b); // 4
28+
29+
// 4. Mixing String and Number
30+
const c = 12;
31+
const d = "3";
32+
33+
console.log("\n=== Mixing Number and String ===");
34+
console.log("c + d:", c + d); // "123"
35+
console.log("c - d:", c - d); // 9
36+
console.log("c * d:", c * d); // 36
37+
console.log("c / d:", c / d); // 4
38+
39+
// 5. Invalid String Arithmetic
40+
const str1 = "apple";
41+
const str2 = "mango";
42+
43+
console.log("\n=== Non-numeric String Operations ===");
44+
console.log("str1 + str2:", str1 + str2); // "applemango"
45+
console.log("str1 - str2:", str1 - str2); // NaN
46+
console.log("str1 * str2:", str1 * str2); // NaN
47+
console.log("str1 / str2:", str1 / str2); // NaN
48+
49+
// 6. Best Practice: Convert String to Number
50+
console.log("\n=== Converting Input to Number ===");
51+
const userInput = "42"; // Simulating user input
52+
const numberInput = Number(userInput); // Convert to number
53+
54+
if (!isNaN(numberInput)) {
55+
console.log(`Converted and added: ${numberInput + 8}`); // 50
56+
} else {
57+
console.log("Invalid input");
58+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Arithmetic Operators in JavaScript
2+
3+
// 1. Basic Arithmetic Operations
4+
const x = 12;
5+
const y = 3;
6+
7+
console.log("=== Basic Arithmetic Operations ===");
8+
console.log("Addition:", x + y); // 15
9+
console.log("Subtraction:", x - y); // 9
10+
console.log("Multiplication:", x * y); // 36
11+
console.log("Division:", x / y); // 4
12+
console.log("Remainder:", x % y); // 0
13+
console.log("Exponentiation x ** 2:", x ** 2); // 144
14+
console.log("Exponentiation y ** 3:", y ** 3); // 27
15+
16+
// 2. String Addition
17+
const a = "12";
18+
const b = "3";
19+
20+
console.log("\n=== String Addition (Concatenation) ===");
21+
console.log("a + b:", a + b); // "123" (string concatenation)
22+
23+
// 3. Other Operations with String Numbers
24+
console.log("\n=== Arithmetic with String Numbers ===");
25+
console.log("a - b:", a - b); // 9
26+
console.log("a * b:", a * b); // 36
27+
console.log("a / b:", a / b); // 4
28+
29+
// 4. Mixing String and Number
30+
const c = 12;
31+
const d = "3";
32+
33+
console.log("\n=== Mixing Number and String ===");
34+
console.log("c + d:", c + d); // "123"
35+
console.log("c - d:", c - d); // 9
36+
console.log("c * d:", c * d); // 36
37+
console.log("c / d:", c / d); // 4
38+
39+
// 5. Invalid String Arithmetic
40+
const str1 = "apple";
41+
const str2 = "mango";
42+
43+
console.log("\n=== Non-numeric String Operations ===");
44+
console.log("str1 + str2:", str1 + str2); // "applemango"
45+
console.log("str1 - str2:", str1 - str2); // NaN
46+
console.log("str1 * str2:", str1 * str2); // NaN
47+
console.log("str1 / str2:", str1 / str2); // NaN
48+
49+
// 6. Best Practice: Convert String to Number
50+
console.log("\n=== Converting Input to Number ===");
51+
const userInput = "42"; // Simulating user input
52+
const numberInput = Number(userInput); // Convert to number
53+
54+
if (!isNaN(numberInput)) {
55+
console.log(`Converted and added: ${numberInput + 8}`); // 50
56+
} else {
57+
console.log("Invalid input");
58+
}

.history/pracrice/Concatenation and Template Literal_20250701033150.js

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// String Concatenation example
2+
let name = "Henok"; // Declare variable for name
3+
let age = 22; // Declare variable for age
4+
5+
// Create message by joining strings using + operator
6+
let messageConcat = "My name is " + name + " and I am " + age + " years old.";
7+
console.log(messageConcat); // Output: My name is Henok and I am 22 years old.
8+
9+
// Template Literal example
10+
// Use backticks and ${} to embed variables directly
11+
let messageTemplate = `My name is ${name} and I am ${age} years old.`;
12+
console.log(messageTemplate); // Output: My name is Henok and I am 22 years old.
13+
14+
// Multiline Template Literal example
15+
// Allows easy multiline strings without + or \n
16+
let multilineMessage = `My name is ${name}.
17+
I am ${age} years old.
18+
I love coding and learning new things.`;
19+
console.log(multilineMessage);
20+
/* Output:
21+
My name is Henok.
22+
I am 22 years old.
23+
I love coding and learning new things.
24+
*/

0 commit comments

Comments
 (0)