22 Lab start */
33
44/* EXERCISE 1
5- TODO: Modify the code to add types to the variable declarations.
5+ TODO: Modify the code to add types to the variable declarations.
66 The resulting JavaScript should look the same as the original example when you're done. */
77
8- let firstName ;
9- let lastName ;
10- let fullName ;
11- let age ;
12- let ukCitizen ;
8+ let firstName : string ;
9+ let lastName : string ;
10+ let fullName : string ;
11+ let age : number ;
12+ let ukCitizen : boolean ;
1313
14- firstName = ' Rebecca' ;
15- lastName = ' Smith' ;
14+ firstName = " Rebecca" ;
15+ lastName = " Smith" ;
1616age = 42 ;
1717ukCitizen = false ;
18- fullName = firstname + " " + lastname ;
18+ fullName = firstName + " " + lastName ;
1919
2020if ( ukCitizen ) {
21- console . log ( "My name is " + fullName + ", I'm " + age + ", and I'm a citizen of the United Kingdom." ) ;
21+ console . log (
22+ "My name is " +
23+ fullName +
24+ ", I'm " +
25+ age +
26+ ", and I'm a citizen of the United Kingdom."
27+ ) ;
2228} else {
23- console . log ( "My name is " + fullName + ", I'm " + age + ", and I'm not a citizen of the United Kingdom." ) ;
29+ console . log (
30+ "My name is " +
31+ fullName +
32+ ", I'm " +
33+ age +
34+ ", and I'm not a citizen of the United Kingdom."
35+ ) ;
2436}
2537
2638/* EXERCISE 2
27- TODO: You can use types to ensure operation outcomes. Run the code as is and then modify
28- it to have strongly typed variables. Then, address any errors you find so that the result
39+ TODO: You can use types to ensure operation outcomes. Run the code as is and then modify
40+ it to have strongly typed variables. Then, address any errors you find so that the result
2941 returned to a is 12. */
3042
31- let x ;
32- let y ;
33- let a ;
43+ enum num {
44+ five = 5 ,
45+ }
46+ let x : num ;
47+ let y : number ;
48+ let a : number ;
3449
35- x = ' five' ;
50+ x = num . five ;
3651y = 7 ;
3752a = x + y ;
3853
3954console . log ( a ) ;
4055
4156/* EXERCISE 3
42- TODO: In the following code, implement an enum type called Season that represents
43- the constants "Fall", "Winter", "Spring", and "Summer". Then, update the function so
44- you can pass in the season by referencing an item in the enum, for example
57+ TODO: In the following code, implement an enum type called Season that represents
58+ the constants "Fall", "Winter", "Spring", and "Summer". Then, update the function so
59+ you can pass in the season by referencing an item in the enum, for example
4560 Season.Fall, instead of the literal string "Fall". */
4661
47- function whichMonths ( season ) {
48-
49- let monthsInSeason : string ;
50-
51- switch ( season ) {
52- case "Fall" :
53- monthsInSeason = "September to November" ;
54- break ;
55- case "Winter" :
56- monthsInSeason = "December to February" ;
57- break ;
58- case "Spring" :
59- monthsInSeason = "March to May" ;
60- break ;
61- case "Summer" :
62- monthsInSeason = "June to August" ;
63- }
64-
65- return monthsInSeason ;
62+ enum Season {
63+ "Fall" ,
64+ "Winter" ,
65+ "Spring" ,
66+ "Summer" ,
67+ }
68+
69+ function whichMonths ( season : Season ) {
70+ let monthsInSeason : string ;
71+
72+ switch ( season ) {
73+ case Season . Fall :
74+ monthsInSeason = "September to November" ;
75+ break ;
76+ case Season . Winter :
77+ monthsInSeason = "December to February" ;
78+ break ;
79+ case Season . Spring :
80+ monthsInSeason = "March to May" ;
81+ break ;
82+ case Season . Summer :
83+ monthsInSeason = "June to August" ;
84+ }
85+
86+ return monthsInSeason ;
6687}
6788
68- console . log ( whichMonths ( " Fall" ) ) ;
89+ console . log ( whichMonths ( Season . Fall ) ) ;
6990
7091/* EXERCISE 4
7192 TODO: Declare the array as the type to match the type of the items in the array. */
7293
73- let randomNumbers ;
74- let nextNumber ;
75-
76- for ( let i = 0 ; i < 10 ; i ++ ) {
77- nextNumber = Math . floor ( Math . random ( ) * ( 100 - 1 ) ) + 1 ;
78- randomNumbers . push ( nextNumber ) ;
79- }
80-
81- console . log ( randomNumbers ) ;
82-
83-
94+ let randomNumbers : number [ ] = [ ] ;
95+ let nextNumber : number ;
96+
97+ for ( let i = 0 ; i < 10 ; i ++ ) {
98+ nextNumber = Math . floor ( Math . random ( ) * ( 100 - 1 ) ) + 1 ;
99+ randomNumbers . push ( nextNumber ) ;
100+ }
101+
102+ console . log ( randomNumbers ) ;
0 commit comments