Skip to content

Commit 84feb14

Browse files
authored
Merge pull request mouredev#6316 from Sac-Corts/main
#00, #1, #2, #3, #4, #5, #6, #7, #8, #9, #10, #11, #12, #13, #14, #15, #16, #17, #18, #19, #20, #21, #22, #23, #24, #25, #26, #27, #28, #29, mouredev#30 - TypeScript
2 parents 58d16ea + 07f4a55 commit 84feb14

File tree

31 files changed

+2695
-0
lines changed

31 files changed

+2695
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// https://www.typescriptlang.org/
2+
// Single-line comment
3+
4+
/*
5+
* Multi-line comment
6+
* Explaining the purpose of the code
7+
* or any other revelant information
8+
*/
9+
10+
// Declaring a variable
11+
let language: string = "TypeScript";
12+
13+
// Declaring a constant
14+
const version: string = "5.5.4";
15+
16+
// Declaring variables representing different primitive data types
17+
let text: string = "This is a string"; // String
18+
let number: number = 43; // Number (integer or decimal)
19+
let boolean: boolean = true; // Boolean
20+
21+
// Undefined and null are also considered types in TypeScript
22+
let undefinedVar: undefined = undefined; //Undefined
23+
let nullVar: null = null; // Null
24+
25+
console.log(`Hello, ${language}!`);
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Arithmetic Operators
2+
let c: number = 10;
3+
let b: number = 5;
4+
console.log("Addition:", c + b);
5+
console.log("Subtraction:", c - b);
6+
console.log("Multiplication:", c * b);
7+
console.log("Division:", c / b);
8+
console.log("Modulus:", c % b);
9+
console.log("Exponentiation:", c ** b);
10+
11+
// Logical Operators
12+
let isTrue: boolean = true;
13+
let isFalse: boolean = false;
14+
console.log("AND (&&):", isTrue && isFalse);
15+
console.log("OR (||):", isTrue || isFalse);
16+
console.log("NOT (!):", !isTrue);
17+
18+
// Comparison Operators
19+
console.log("Equal (==):", c == b);
20+
console.log("Not equal (!=):", c != b);
21+
console.log("Strict equal (===):", c === b);
22+
console.log("Strict not equal (!==):", c !== b);
23+
console.log("Greater than (>):", c > b);
24+
console.log("Less than (<):", c < b);
25+
console.log("Greater than or equal (>=):", c >= b);
26+
console.log("Less than or equal (<=):", c <= b);
27+
28+
// Assignment Operators
29+
let x: number = 10;
30+
x += 5;
31+
console.log("x after x += 5:", x);
32+
x -= 3;
33+
console.log("x after x -= 5:", x);
34+
x *= 2;
35+
console.log("x after x *= 2:", x);
36+
x /= 4;
37+
console.log("x after x /= 4:", x);
38+
x **= 2;
39+
console.log("x after x **= 2:", x);
40+
x %= 2;
41+
console.log("x after x %= 2:", x);
42+
43+
// Bitwise Operators
44+
let bitA: number = 5; // Binary: 0101
45+
let bitB: number = 3; // Binary: 0011
46+
console.log("Bitwise AND(&):", bitA & bitB);
47+
console.log("Bitwise OR(|):", bitA | bitB);
48+
console.log("Bitwise XOR(^):", bitA ^ bitB);
49+
console.log("Bitwise NOT(~):", ~bitA);
50+
console.log("Left shift(<<):", bitA << 1);
51+
console.log("Right shift(>>):", bitA >> 1);
52+
53+
// Control Structures //
54+
// If-else statement
55+
if (c > b) {
56+
console.log("c is greater than b");
57+
} else {
58+
console.log("c is not greater than b");
59+
}
60+
61+
// Ternary Operator
62+
let max = c > b ? c : b;
63+
console.log("Max value:", max);
64+
65+
// Switch-case statement
66+
let day: number = 2;
67+
switch(day) {
68+
case 1:
69+
console.log("It's Monday");
70+
break;
71+
case 2:
72+
console.log("It's Tuesday");
73+
break;
74+
case 3:
75+
console.log("It's Wednesday");
76+
break;
77+
}
78+
79+
// While loop
80+
let _count: number = 0;
81+
while (_count < 3) {
82+
console.log("While loop count:", _count);
83+
_count++;
84+
}
85+
86+
// For loop
87+
for (let i = 0; i < 3; i++) {
88+
console.log("For loop iteration:", i);
89+
}
90+
91+
// Do-while loop
92+
let doCount: number = 0;
93+
do {
94+
console.log("Do-while loop count:", doCount);
95+
doCount++;
96+
} while (doCount < 3);
97+
98+
// Try-catch (Exception Handling)
99+
try {
100+
let result = c / 0;
101+
if (!isFinite(result)) {
102+
throw new Error("Cannot divide by zero");
103+
}
104+
} catch (error) {
105+
if (error instanceof Error) {
106+
console.log(error.message);
107+
}
108+
} finally {
109+
console.log("Finally block executed");
110+
}
111+
112+
// *** Extra Exercise *** //
113+
114+
for (let i = 10; i <= 55; i++) {
115+
if (i % 2 == 0 && i % 3 !== 0 && i !== 16) {
116+
console.log(i);
117+
}
118+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Function with no parameters and no return
2+
function greet(): void {
3+
console.log("Hello, welcome to TypeScript!");
4+
}
5+
6+
// Function with one parameter and no return
7+
function greetUser(name: string): void {
8+
console.log(`Hello, ${name}!`);
9+
}
10+
11+
// Function with multiple parameters and no return
12+
function addNumbers(a: number, b: number): void {
13+
console.log(`The sum of ${a} and ${b} is ${a + b}`);
14+
}
15+
16+
// Function with one parameter and a return value
17+
function square(num: number): number {
18+
return num * num;
19+
}
20+
21+
// Function with multiple parameters and a return value
22+
function multiplyNumbers(a: number, b: number): number {
23+
return a * b;
24+
}
25+
26+
// Nested Functions
27+
function outerFunction(): void {
28+
console.log("This is the outer function");
29+
30+
// Inner function
31+
function innerFunction(): void {
32+
console.log("This is the inner function");
33+
}
34+
35+
innerFunction();
36+
}
37+
38+
// Built-in Functions
39+
function calculateCircleArea(radius: number): number {
40+
return Math.PI * Math.pow(radius, 2);
41+
}
42+
43+
// Testing local and global variables
44+
// Global variable
45+
let globalVar = "I'm a global variable";
46+
47+
function testVariableScope(): void {
48+
// Local variable
49+
let localVar = "I'm a local variable";
50+
51+
console.log(globalVar);
52+
console.log(localVar);
53+
54+
globalVar = "Global variable modified inside the function";
55+
}
56+
57+
greet(); // Function without parameters and no return
58+
greetUser("Isaac"); // Function with one parameter and no return
59+
addNumbers(5, 7); // Function with multiple parameters and no return
60+
console.log(`Square of 4 is: ${square(4)}`); // Function with one parameter and return value
61+
console.log(`Multiplication of 3 and 6 is: ${multiplyNumbers(3, 6)}`); // Function with multiple parameters and return value
62+
63+
outerFunction(); // Calls outer and inner function
64+
65+
console.log(`Area of circle with radius 5 is: ${calculateCircleArea(5)}`); // Uses Math functions
66+
67+
testVariableScope();
68+
console.log(globalVar);
69+
// console.log(localVar); Error: localVar is not defined outside the function
70+
71+
72+
// *** Extra Exercise *** //
73+
function printNumbersWithText(string1: string, string2: string): number {
74+
let counter = 0;
75+
76+
for (let i = 1; i <= 100; i++) {
77+
if (i % 3 === 0 && i % 5 === 0) {
78+
console.log(string1 + string2);
79+
} else if (i % 3 === 0) {
80+
console.log(string1);
81+
} else if (i % 5 === 0) {
82+
console.log(string2);
83+
} else {
84+
console.log(i);
85+
counter++;
86+
}
87+
}
88+
return counter;
89+
}
90+
91+
const times = printNumbersWithText("Fizz", "Buzz");
92+
console.log(`The number was printed ${times} times`);

0 commit comments

Comments
 (0)