1+ /**
2+ * @author : Kronstadt-Lambda
3+ * @version : 1.0
4+ */
5+
6+ // Initialize variables
7+ let float_a = 2.0 ;
8+ let float_b = 2.0 ;
9+ let bit_a = 0b00000101 ;
10+ let bit_b = 0b00000110 ;
11+ const day = "Wendesday" ;
12+ let edad = 1 ;
13+ let isBool ;
14+ const person = { name : "Jhon" , age : 30 } ;
15+ const numbers = [ 1 , 2 , 3 ] ;
16+
17+ /**
18+ * Operators in JavaScript
19+ */
20+ // Arithmetic operators
21+ console . log ( "Addition:" , float_a + float_b ) ;
22+ console . log ( "Subtraction:" , float_a - float_b ) ;
23+ console . log ( "Multiplication:" , float_a * float_b ) ;
24+ console . log ( "Division:" , float_a / float_b ) ;
25+ console . log ( "Exponentiation:" , float_a ** float_b ) ;
26+ console . log ( "Modulus:" , float_a % float_b ) ;
27+ console . log ( "Pre-Increment:" , ++ float_a ) ;
28+ console . log ( "Pre-Decrement:" , -- float_a ) ;
29+ console . log ( "Post-increment:" , float_a ++ ) ;
30+ console . log ( "Post-decrement:" , float_a -- ) ;
31+
32+ // Assignment operators
33+ console . log ( "Simple Assignment:" , float_a = float_b ) ;
34+ console . log ( "Addition Assignment:" , float_a += float_b ) ;
35+ console . log ( "Subtraction Assignment:" , float_a -= float_b ) ;
36+ console . log ( "Multiplication Assignment:" , float_a *= float_b ) ;
37+ console . log ( "Division Assignment:" , float_a /= float_b ) ;
38+ console . log ( "Exponentiation Assignment:" , float_a **= float_b ) ;
39+ console . log ( "Modulus Assignment:" , float_a %= float_b ) ;
40+ console . log ( "Bitwise AND Assignment:" , bit_a &= bit_b ) ;
41+ console . log ( "Bitwise OR Assignment:" , bit_a |= bit_b ) ;
42+ console . log ( "Bitwise XOR Assignment:" , bit_a ^= bit_b ) ;
43+ console . log ( "Left Shift Assignment:" , bit_a <<= bit_b ) ;
44+ console . log ( "Right Shift Assignment:" , bit_a >>= bit_b ) ;
45+
46+ // Comparison operators
47+ console . log ( "Equal to:" , float_a == float_b ) ;
48+ console . log ( "Not equal to:" , float_a != float_b ) ;
49+ console . log ( "Strict equal (value and type) to:" , float_a === float_b ) ;
50+ console . log ( "Strict not equal (value and type) to:" , float_a !== float_b ) ;
51+ console . log ( "Greater than:" , float_a > float_b ) ;
52+ console . log ( "Less than:" , float_a < float_b ) ;
53+ console . log ( "Greater than or equal to:" , float_a >= float_b ) ;
54+ console . log ( "Less than or equal to:" , float_a <= float_b ) ;
55+
56+ // Logical operators
57+ console . log ( "Logical AND:" , true && false ) ;
58+ console . log ( "Logical OR:" , true || false ) ;
59+ console . log ( "Logical NOT:" , ! true ) ;
60+
61+ // Bitwise operators
62+ console . log ( "Bitwise AND:" , bit_a & bit_b ) ;
63+ console . log ( "Bitwise OR:" , bit_a | bit_b ) ;
64+ console . log ( "Bitwise XOR:" , bit_a ^ bit_b ) ;
65+ console . log ( "Bitwise NOT:" , ~ bit_a ) ;
66+ console . log ( "Left Shift:" , bit_a << 1 ) ;
67+ console . log ( "Right Shift:" , bit_a >> 1 ) ;
68+ console . log ( "Zero-fill Right Shift:" , bit_a >>> 1 ) ;
69+
70+ // Identity operators
71+ console . log ( "Equal value and type:" , float_a === float_b ) ;
72+ console . log ( "Not equal value or not equal type:" , float_a !== float_b ) ;
73+
74+ // Membership operators
75+ console . log ( "Is in:" , 'name' in person ) ; // Verify if the property exists in the object
76+ console . log ( "Is not in:" , 'lastname' in person ) ; // Verify if the property does not exist in the object
77+ console . log ( "Is in:" , numbers . includes ( 2 ) ) ; // Verify if the value exists in the array
78+
79+ /**
80+ * Control structures in JavaScript
81+ */
82+ // If, if-else, else statement
83+ if ( float_a === float_b ) {
84+ console . log ( "The numbers are equal." ) ;
85+ } else if ( float_a > float_b ) {
86+ console . log ( "float_a is greater than float_b." ) ;
87+ } else {
88+ console . log ( "float_a is less than float_b." ) ;
89+ }
90+ // Switch statement
91+ switch ( day ) {
92+ case "Monday" :
93+ console . log ( "Today is Monday." ) ;
94+ break ; // Exit the switch statement to avoid executing the next cases
95+ case "Tuesday" :
96+ console . log ( "Today is Tuesday." ) ;
97+ break ; // Exit the switch statement to avoid executing the next cases
98+ case "Wednesday" :
99+ console . log ( "Today is Wednesday." ) ;
100+ break ; // Exit the switch statement to avoid executing the next cases
101+ case "Thursday" :
102+ console . log ( "Today is Thursday." ) ;
103+ break ; // Exit the switch statement to avoid executing the next cases
104+ case "Friday" :
105+ console . log ( "Today is Friday." ) ;
106+ break ; // Exit the switch statement to avoid executing the next cases
107+ default : // If no case matches
108+ console . log ( "It's the weekend." ) ;
109+ break ; // Exit the switch statement to avoid executing the next cases
110+ }
111+ // For loop
112+ for ( let i = 10 ; i >= 0 ; i -- ) {
113+ console . log ( "The loop end in ..." , i ) ;
114+ }
115+ // While loop
116+ while ( edad < 18 ) {
117+ console . log ( "I am " , edad , " years old." ) ;
118+ edad ++ ;
119+ }
120+ // Do-while loop
121+ do {
122+ isBool = false ;
123+ console . log ( "At least one execution." ) ;
124+ } while ( isBool ) ;
125+ // For-in loop: Usefull for objects
126+ for ( let key in person ) {
127+ console . log ( key , ":" , person [ key ] ) ;
128+ }
129+ // For-of loop: Usefull for arrays
130+ for ( let number of numbers ) {
131+ console . log ( "Number:" , number ) ;
132+ }
133+ // Break statement
134+ for ( let i = 0 ; i < 10 ; i ++ ) {
135+ if ( i === 5 ) {
136+ console . log ( "Break at:" , i ) ;
137+ break ; // Exit the loop
138+ }
139+ console . log ( "i = :" , i ) ;
140+ }
141+ // Continue statement
142+ for ( let i = 0 ; i < 10 ; i ++ ) {
143+ if ( i === 5 ) {
144+ console . log ( "Continue (skip) at:" , i ) ;
145+ continue ; // Skip the current iteration
146+ }
147+ console . log ( "Cube of" , i , "is" , i ** 3 ) ;
148+ }
149+ // Label statement: Exit an outer loop
150+ outerloop: for ( let i = 0 ; i < 5 ; i ++ ) {
151+ console . log ( "Outerloop:" , i ) ;
152+ innerloop: for ( let j = 0 ; j < 5 ; j ++ ) {
153+ if ( j === 3 ) {
154+ break outerloop; // Exit the outer loop
155+ }
156+ console . log ( "Innerloop:" , j ) ;
157+ }
158+ }
159+ // Return statement: Exit a function
160+ function add ( a , b ) {
161+ return a + b ;
162+ }
163+ // try-catch-finally statement: Handle exceptions
164+ try {
165+ console . log ( numbers [ 3 ] ) ;
166+ } catch ( error ) {
167+ console . error ( "An error occurred:" , error ) ;
168+ } finally {
169+ console . log ( "Finally block." ) ;
170+ }
171+
172+ /**
173+ * Extra exercises
174+ * Create a program that prints to the console all numbers between 10 and 55 (inclusive), even, and that are neither 16 nor multiples of 3
175+ */
176+
177+ for ( let i = 10 ; i <= 55 ; i ++ ) {
178+ if ( i % 2 === 0 && i !== 16 && i % 3 !== 0 ) {
179+ console . log ( "The solution is: " , i ) ;
180+ }
181+ }
0 commit comments