From bd4a395d82bf449d9f8b4ee7e1108f8fa85f909a Mon Sep 17 00:00:00 2001 From: daniel ashley Date: Sat, 27 Oct 2018 18:00:21 -0400 Subject: [PATCH 1/2] In Progress Midterm --- js/app.js | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 113 insertions(+), 1 deletion(-) diff --git a/js/app.js b/js/app.js index 7bd1016..f65e13e 100644 --- a/js/app.js +++ b/js/app.js @@ -23,7 +23,19 @@ */ // implement function here +const newShoppingListItem = (item, price)=>{ +const shoppingListItem = { + +"item" : item, +"price": price, + + +} + +return shoppingListItem +} +let sList = newShoppingListItem() // TEST describe('1. newShoppingListItem', () => { it('should return an object with item and price attributes', () => { @@ -54,6 +66,17 @@ // implement function here +const addToShoppingList =(item, list=[])=>{ + + list.push(item) + return list + + + +} + +console.log(addToShoppingList()) + // TEST describe('2. addToShoppingList', () => { @@ -86,7 +109,21 @@ */ // implement function here +const removeFromShoppingList= (list)=>{ +if(list.legnth === 0){ + + return list +} + +else{ + list.pop() + return list + +} + + +} // TEST describe('3. removeFromShoppingList', () => { it('should remove from the end of the list', () => { @@ -121,6 +158,24 @@ // implement function here +const removeFirstItem = (list)=>{ + + if(list.legnth === 0){ + + return list + } + + else{ + list.shift() + return list + + } + +} + + + + // TEST describe('4. removeFirstItem', () => { it('should remove from the end of the list', () => { @@ -158,7 +213,16 @@ */ // implement function here - + const removeNthItem = (i, list)=>{ + if(typeof i !== "number" || i < 0 || i > list.length){ + + throw new Error("Input a number") + } + list.splice(list.indexOf(list[i]),1) + + return list + + } // TEST describe('5. removeNthItem', () => { it('should remove i-th item from list', () => { @@ -228,7 +292,19 @@ */ // implement function here +const removeNItems = (i,num,list)=>{ +if(i<0 || typeof i !== "number" || typeof num !=="number" || i+num > list.length|| num>list.length){ + + throw new Error("Input Appropriate Number") +} + +list.splice(list.indexOf(list[i]),list.indexOf(list[i+num])) + + +return list + +} // TEST describe('6. removeNItems', () => { it('should remove i-th item from list', () => { @@ -307,6 +383,26 @@ */ // implement function here +const smartRemoveItems =(i,list)=>{ + +if(i <0){ + list.pop(i) + return list + } + else if(i>list.length){ + return list + + + } + else if(i >0){ + list.shift(i) + return list + } + +} + + + // TEST describe('7. smartRemoveItems', () => { @@ -385,7 +481,23 @@ */ // implement function here + const spliceItem = (item,i,list)=>{ + if(typeof item !== "object"){ + throw new Error("Fix Object") + } + + if(i > list.length){ + list.push(Object.values(item).join()) + + } + if(i < 0){ + + list.unshift(Object.values(item).join()) + } + list.splice(i,0, Object.values(item).join()) + return list + } // TEST describe('8. spliceItem', () => { it('should throw an error if item is not valid', () => { From c8df1a86953208e8104cb81505539ff905e1d1a6 Mon Sep 17 00:00:00 2001 From: daniel ashley Date: Sun, 28 Oct 2018 10:00:35 -0400 Subject: [PATCH 2/2] Midterm --- js/app.js | 144 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 131 insertions(+), 13 deletions(-) diff --git a/js/app.js b/js/app.js index f65e13e..e5f642d 100644 --- a/js/app.js +++ b/js/app.js @@ -486,18 +486,20 @@ if(i <0){ throw new Error("Fix Object") } - if(i > list.length){ - list.push(Object.values(item).join()) - - } - if(i < 0){ - - list.unshift(Object.values(item).join()) + else if(i > list.length){ + list.push(item) + return list } - list.splice(i,0, Object.values(item).join()) - - return list - } + else if(i < 0){ + + list.unshift(item) + return list + } + + // list.splice(i,0, Object.values(item).join()) + return list.slice(0,i).concat(item).concat(list.slice(i)) + } + // TEST describe('8. spliceItem', () => { it('should throw an error if item is not valid', () => { @@ -565,6 +567,34 @@ if(i <0){ // implement function here + + const spliceItems=(items,i,list)=>{ + items.forEach((e)=>{ + + if( typeof e !== "object"){ + throw new Error("Fix Object") + } + } + ) + + if(i > list.length){ + return list.concat(items) + + } + else if(i < 0){ + + return items.concat(list) + + } + else if(items.length === 0){ return list} + // list.splice(i,0, Object.values(item).join()) + return list.slice(0,i).concat(items).concat(list.slice(i)) + } + + + + + // TEST describe('9. spliceItems', () => { it('should throw an error if item is not valid', () => { @@ -644,6 +674,32 @@ if(i <0){ */ // implement function here +const combineLists = (items1,items2)=>{ + + items1.forEach((e)=>{ + + if( typeof e !== "object"){ + throw new Error("Fix Object") + } + } + ) + items2.forEach((e)=>{ + + if( typeof e !== "object"){ + throw new Error("Fix Object") + } + } + ) + +const list = items1.concat(items2) + + +return list + +} + + + // TEST describe('10. combineLists', () => { @@ -696,7 +752,28 @@ if(i <0){ */ // implement function here - + const splitListAt =(i,list)=>{ + let list1 = [] + let list2 =[] + + if(i <= 0){ + list1 = [...list] + + } + + + else if(i>list.length){ + list2 = [...list] + + + } + else{ + list1 = list.slice(0,i+1) + list2 = list.slice(i+1) + } + return [list1 ,list2] + } + // TEST describe('11. splitListAt', () => { it('should break list into two at index', () => { @@ -776,7 +853,14 @@ if(i <0){ */ // implement function here +const canExpressCheckout = (list)=>{ + +if(list.length< 10){ + return true +} + +} // TEST describe('12. canExpressCheckout', () => { it('should return true if num items < 10', () => { @@ -804,7 +888,14 @@ if(i <0){ */ // implement function here - + const computeSum = (list)=>{ + let sum = 0 + for(let i = 0; i { it('should return sum of all item prices in array', () => { @@ -836,7 +927,16 @@ if(i <0){ - note that tax is passed in as percent not decimal */ +const computeSumWithTax =(list, taxRate)=>{ + + let sum = 0 + for(let i = 0; i{ + let list2 = list.slice(i,j+1) + + if(i> j){ + + throw new Error("Input Better Values") +} +if(i<0 || j<0 || i > list.length || j>list.length){ + throw new Error("Input Better Values") + +} +let sum = 0 + for(let i = 0; i { it('should throw error if i > j', () => {