@@ -16,48 +16,56 @@ you have more than 3 items in your shopping cart the first item gets taken out.
1616
17172. Confirm that your code passes the unit tests.
1818-----------------------------------------------------------------------------*/
19- const shoppingCart = [ ' bananas' , ' milk' ] ;
19+ const shoppingCart = [ " bananas" , " milk" ] ;
2020
2121// ! Function to be tested
22- function addToShoppingCart ( /* parameters go here */ ) {
22+ function addToShoppingCart ( item /* parameters go here */ ) {
2323 // TODO complete this function
24+ if ( ! item ) {
25+ return `You bought ${ shoppingCart . join ( ", " ) } !` ;
26+ }
27+ // shoppingCart.length > 2 ? shoppingCart.splice(0, 1) : shoppingCart;
28+ shoppingCart . length > 2 && shoppingCart . splice ( 0 , 1 ) ;
29+
30+ shoppingCart . push ( item ) ;
31+ return `You bought ${ shoppingCart . join ( ", " ) } !` ;
2432}
2533
2634// ! Test functions (plain vanilla JavaScript)
2735function test1 ( ) {
2836 console . log (
29- ' Test 1: addShoppingCart() called without an argument should leave the shopping cart unchanged'
37+ " Test 1: addShoppingCart() called without an argument should leave the shopping cart unchanged"
3038 ) ;
31- const expected = ' You bought bananas, milk!' ;
39+ const expected = " You bought bananas, milk!" ;
3240 const actual = addToShoppingCart ( ) ;
3341 console . assert ( actual === expected ) ;
3442}
3543
3644function test2 ( ) {
37- console . log ( ' Test 2: addShoppingCart() should take one parameter' ) ;
45+ console . log ( " Test 2: addShoppingCart() should take one parameter" ) ;
3846 const expected = 1 ;
3947 const actual = addToShoppingCart . length ;
4048 console . assert ( actual === expected ) ;
4149}
4250
4351function test3 ( ) {
44- console . log ( ' Test 3: `chocolate` should be added' ) ;
45- const expected = ' You bought bananas, milk, chocolate!' ;
46- const actual = addToShoppingCart ( ' chocolate' ) ;
52+ console . log ( " Test 3: `chocolate` should be added" ) ;
53+ const expected = " You bought bananas, milk, chocolate!" ;
54+ const actual = addToShoppingCart ( " chocolate" ) ;
4755 console . assert ( actual === expected ) ;
4856}
4957
5058function test4 ( ) {
51- console . log ( ' Test 4: `waffles` should be added and `bananas` removed' ) ;
52- const expected = ' You bought milk, chocolate, waffles!' ;
53- const actual = addToShoppingCart ( ' waffles' ) ;
59+ console . log ( " Test 4: `waffles` should be added and `bananas` removed" ) ;
60+ const expected = " You bought milk, chocolate, waffles!" ;
61+ const actual = addToShoppingCart ( " waffles" ) ;
5462 console . assert ( actual === expected ) ;
5563}
5664
5765function test5 ( ) {
58- console . log ( ' Test 5: `tea` should be added and `milk` removed' ) ;
59- const expected = ' You bought chocolate, waffles, tea!' ;
60- const actual = addToShoppingCart ( ' tea' ) ;
66+ console . log ( " Test 5: `tea` should be added and `milk` removed" ) ;
67+ const expected = " You bought chocolate, waffles, tea!" ;
68+ const actual = addToShoppingCart ( " tea" ) ;
6169 console . assert ( actual === expected ) ;
6270}
6371
0 commit comments