1
1
// 11_challenge.js
2
2
3
3
/*
4
- Challenge: Compound Variables Calculator
4
+
5
+ Challenge 1 : Compound Variables Calculator
5
6
6
7
Objective: Create a program that uses all types of variables (number, string, boolean) and performs compound assignment operations on them.
7
8
@@ -14,35 +15,63 @@ Instructions:
14
15
Files to Reference: 01A_compound_assignment.js, 01_variables.js, 02_declare_intialize.js, 03_string_vars.js
15
16
*/
16
17
18
+ // Number operations
17
19
let myNum = 20 ;
20
+
21
+ myNum += 5 ; // add 5 to myNum
22
+ console . log ( myNum ) ; // Output: 25
23
+
24
+ myNum -= 3 ; // subtract 3 from myNum
25
+ console . log ( myNum ) ; // Output: 22
26
+
27
+ myNum *= 2 ; // multiply myNum by 2
28
+ console . log ( myNum ) ; // Output: 44
29
+
30
+ myNum /= 4 ; // divide myNum by 4
31
+ console . log ( myNum ) ; // Output: 11
32
+
33
+ myNum %= 4 ; // get the remainder of myNum divided by 4
34
+ console . log ( myNum ) ; // Output: 3
35
+
36
+ // String operations
18
37
let myStr = "Hello" ;
38
+ myStr += " World" ; // concatenate myStr with " World"
39
+ console . log ( myStr ) ;
40
+
41
+ // Boolean operations
19
42
let myBool = false ;
20
43
21
- // Number operations
44
+ myBool &&= false ; // logical AND operation
45
+ console . log ( "After &&= false, bool:" , myBool ) ; // Output: false
22
46
23
- myNum += 5 ;
24
- console . log ( myNum ) // Output : 25
47
+ myBool ||= true ; // logical OR operation
48
+ console . log ( "After ||= true, bool:" , myBool ) ; // Output: true
25
49
26
- myNum -= 3 ;
27
- console . log ( myNum ) // Output: 22
28
50
29
- myNum *= 2 ;
30
- console . log ( myNum ) // Output: 44
51
+ /*
52
+ Challenge 2 : Check if Number is a Palindrome
31
53
32
- myNum /= 4 ;
33
- console . log ( myNum ) // Output: 11
54
+ Objective: Write a program that takes a number as input and checks if it is a palindrome (a number that reads the same forwards and backwards).
34
55
35
- myNum %= 4 ;
36
- console . log ( myNum ) // Output: 3
56
+ Instructions:
37
57
38
- // String operations
58
+ - Write a function that takes a number as input and checks if it is a palindrome.
59
+ - Use the function to check if a number is a palindrome.
60
+ - Print the result to the console.
39
61
40
- myStr += " World" ;
41
- console . log ( myStr )
62
+ Files to Reference: 01A_compound_assignment.js, 01_variables.js, 02_declare_intialize.js, 03_string_vars.js
63
+ */
42
64
43
- // Boolean operations
44
- myBool &&= false ;
45
- console . log ( "After &&= false, bool:" , myBool ) ; // Output: false
65
+ function checkIfPalindrome ( num ) {
66
+ let str = num . toString ( ) ;
67
+ let reversedStr = str . split ( "" ) . reverse ( ) . join ( "" ) ;
68
+
69
+ if ( str === reversedStr ) {
70
+ return true ;
71
+ }
72
+ else if ( str !== reversedStr ) {
73
+ return false ;
74
+ }
75
+ }
46
76
47
- myBool ||= true ;
48
- console . log ( "After ||= true, bool:" , myBool ) ; // Output: true
77
+ console . log ( checkIfPalindrome ( 161 ) ) ; // Output: true
0 commit comments