55// Then it should return the total amount in pounds 
66
77function  totalTill ( till )  { 
8+   if  ( typeof  till  !==  "object"  ||  Array . isArray ( till )  ||  till  ===  null )  { 
9+     throw  new  Error ( "Input should be an object" ) ; 
10+   } 
11+ 
12+   const  validPence  =  [ "1p" ,  "5p" ,  "20p" ,  "50p" ] ; 
13+ 
814  let  total  =  0 ; 
915
16+   console . log ( Object . entries ( till ) ) ; 
17+ 
1018  for  ( const  [ coin ,  quantity ]  of  Object . entries ( till ) )  { 
11-     total  +=  coin  *  quantity ; 
19+     if  ( validPence . indexOf ( coin )  ===  - 1 )  { 
20+       continue ; 
21+     } 
22+     total  +=  Number ( coin . slice ( 0 ,  coin . length  -  1 ) )  *  quantity ; 
1223  } 
1324
14-   return  `£${ total  /  100 }  ` ; 
25+   return  `£${ Math . floor ( total  /  100 ) } . ${ String ( total   %   100 ) . padEnd ( 2 ,   0 ) }  ` ; 
1526} 
1627
1728const  till  =  { 
@@ -22,10 +33,18 @@ const till = {
2233} ; 
2334const  totalAmount  =  totalTill ( till ) ; 
2435
36+ module . exports  =  totalTill ; 
37+ 
2538// a) What is the target output when totalTill is called with the till object 
39+ // The target output is a string that state the total value of pound after converted from pence 
40+ // For example: '£4.40' 
2641
2742// b) Why do we need to use Object.entries inside the for...of loop in this function? 
43+ // We need to convert the object till into an array of arrays [ [ '1p', 10 ], [ '5p', 6 ], [ '50p', 4 ], [ '20p', 10 ] ] 
44+ // That will enable the loop process in the for loop. 
2845
2946// c) What does coin * quantity evaluate to inside the for...of loop? 
47+ // coin * quantity was trying to get the value of each group of pence and add to the total 
48+ // However, coin is referencing to first element of each sub array, which is a string that can't do math operation 
3049
3150// d) Write a test for this function to check it works and then fix the implementation of totalTill 
0 commit comments