33
44/* EXERCISE 1
55 TODO: Declare a new function type for the sortDescending and sortAscending functions. */
6-
7-
8- /* TODO: Convert the sortDescending and sortAscending functions to arrow
6+ interface compareFunctionType {
7+ ( a : number , b : number ) : number ;
8+ }
9+ /* TODO: Convert the sortDescending and sortAscending functions to arrow
910 functions. */
1011
11- /* sortDescending is a comparison function that tells the sort method how to sort
12+ /* sortDescending is a comparison function that tells the sort method how to sort
1213 numbers in descending order */
13- function sortDescending ( a , b ) {
14- if ( a > b ) {
14+ const sortDescending : compareFunctionType = ( a , b ) => {
15+ if ( a > b ) {
1516 return - 1 ;
16- } else if ( b > a ) {
17+ } else if ( b > a ) {
1718 return 1 ;
18- } else {
19+ } else {
1920 return 0 ;
20- }
21- }
21+ }
22+ } ;
2223
23- /* sortDescending is a comparison function that tells the sort method how to sort
24+ /* sortDescending is a comparison function that tells the sort method how to sort
2425 numbers in ascending order. */
25- function sortAscending ( a , b ) {
26- if ( a > b ) {
26+ const sortAscending : compareFunctionType = ( a , b ) => {
27+ if ( a > b ) {
2728 return 1 ;
28- } else if ( b > a ) {
29+ } else if ( b > a ) {
2930 return - 1 ;
30- } else {
31+ } else {
3132 return 0 ;
32- }
33- }
33+ }
34+ } ;
3435
35- /* The buildArray function builds an array of unique random numbers containing the number
36- of items based on the number passed to it. The sortOrder parameter determines
36+ /* The buildArray function builds an array of unique random numbers containing the number
37+ of items based on the number passed to it. The sortOrder parameter determines
3738 whether to sort the array in ascending or descending order. */
3839
3940/* TODO: Update the BuildArray function. */
4041
41- function buildArray ( items , sortOrder ) {
42- let randomNumbers = [ ] ;
43- let nextNumber ;
44- for ( let counter = 0 ; counter < items ; counter ++ ) {
45- nextNumber = Math . ceil ( Math . random ( ) * ( 100 - 1 ) ) ;
46- if ( randomNumbers . indexOf ( nextNumber ) === - 1 ) {
47- randomNumbers . push ( nextNumber ) ;
48- } else {
49- counter -- ;
50- }
51- }
52- if ( sortOrder === 'ascending' ) {
53- return randomNumbers . sort ( sortAscending ) ;
42+ function buildArray (
43+ items : number ,
44+ sortOrder : "ascending" | "descending"
45+ ) : number [ ] {
46+ let randomNumbers : number [ ] = [ ] ;
47+ let nextNumber : number ;
48+ for ( let counter = 0 ; counter < items ; counter ++ ) {
49+ nextNumber = Math . ceil ( Math . random ( ) * ( 100 - 1 ) ) ;
50+ if ( randomNumbers . indexOf ( nextNumber ) === - 1 ) {
51+ randomNumbers . push ( nextNumber ) ;
5452 } else {
55- return randomNumbers . sort ( sortDescending ) ;
53+ counter -- ;
5654 }
55+ }
56+ if ( sortOrder === "ascending" ) {
57+ return randomNumbers . sort ( sortAscending ) ;
58+ } else {
59+ return randomNumbers . sort ( sortDescending ) ;
60+ }
5761}
5862
59- let myArray1 = buildArray ( 12 , 'ascending' ) ;
60- let myArray2 = buildArray ( 8 , 'descending' ) ;
63+ let myArray1 = buildArray ( 12 , "ascending" ) ;
64+ let myArray2 = buildArray ( 8 , "descending" ) ;
65+ console . log ( myArray1 ) ;
66+ console . log ( myArray2 ) ;
6167
6268/* EXERCISE 2
6369 TODO: Update the LoanCalculator function. */
6470
65- function loanCalculator ( principle , interestRate , months ) {
66- let interest = interestRate / 1200 ; // Calculates the monthly interest rate
67- let payment ;
68- payment = principle * interest / ( 1 - ( Math . pow ( 1 / ( 1 + interest ) , months ) ) ) ;
69- return payment . toFixed ( 2 ) ;
71+ function loanCalculator (
72+ principle : number ,
73+ interestRate : number ,
74+ months : number = 12
75+ ) {
76+ let interest = interestRate / 1200 ; // Calculates the monthly interest rate
77+ let payment : number ;
78+ payment = ( principle * interest ) / ( 1 - Math . pow ( 1 / ( 1 + interest ) , months ) ) ;
79+ return payment . toFixed ( 2 ) ;
7080}
81+
82+ let myLoan = loanCalculator ( 1000 , 5 ) ;
83+ console . log ( myLoan ) ;
0 commit comments