From 48b8de3a23c71f8894a5250141536852f4ebd0ec Mon Sep 17 00:00:00 2001 From: SaadOcloud Date: Tue, 20 Sep 2022 13:45:07 +0500 Subject: [PATCH 01/13] 1st javascript task' --- Task1.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Task1.js diff --git a/Task1.js b/Task1.js new file mode 100644 index 0000000..de02f4a --- /dev/null +++ b/Task1.js @@ -0,0 +1,17 @@ +function groupcity(array,key){ + return array.filter(a=>{ + return a.city==key + }) +} + +const array= [ + {id:1,name:"Bilal", city:"Lahore"}, + {id:1,name:"Bilal", city:"Lahore"}, + {id:3,name:"Hafsa", city:"Karachi"}, + {id:4,name:"Rehan", city:"Lahore"}, + {id:5,name:"Saqib", city:"Karachi"}, + {id:6,name:"Farhan", city:"Islamabad"} +] +console.log(groupcity(array,"Karachi")) +console.log(groupcity(array,"Lahore")) +console.log(groupcity(array,"Islamabad")) \ No newline at end of file From dd3de54aa83ea7fbbefa03d445dcc33b03b67c5f Mon Sep 17 00:00:00 2001 From: SaadOcloud Date: Tue, 20 Sep 2022 17:30:31 +0500 Subject: [PATCH 02/13] Task 1 correct approach --- Task1.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Task1.js b/Task1.js index de02f4a..0ae7132 100644 --- a/Task1.js +++ b/Task1.js @@ -1,17 +1,18 @@ -function groupcity(array,key){ - return array.filter(a=>{ - return a.city==key - }) +function groupByKey(array, key) { + return array.reduce((hash, obj) => { + if(obj[key] === undefined) return hash; + return Object.assign(hash, { [obj[key]]:( hash[obj[key]] || [] ).concat(obj)}) + }, {}) } const array= [ - {id:1,name:"Bilal", city:"Lahore"}, - {id:1,name:"Bilal", city:"Lahore"}, - {id:3,name:"Hafsa", city:"Karachi"}, - {id:4,name:"Rehan", city:"Lahore"}, - {id:5,name:"Saqib", city:"Karachi"}, - {id:6,name:"Farhan", city:"Islamabad"} -] -console.log(groupcity(array,"Karachi")) -console.log(groupcity(array,"Lahore")) -console.log(groupcity(array,"Islamabad")) \ No newline at end of file + {id:1,name:"Bilal", city:"Lahore"}, + {id:1,name:"Bilal", city:"Lahore"}, + {id:3,name:"Hafsa", city:"Karachi"}, + {id:4,name:"Rehan", city:"Lahore"}, + {id:5,name:"Saqib", city:"Karachi"}, + {id:6,name:"Farhan", city:"Islamabad"} + ]; + +var result = groupByKey(array, 'city'); +console.log(result) From 4c1a6800bcf61b014bc995a920f1505d1973e3d4 Mon Sep 17 00:00:00 2001 From: SaadOcloud Date: Wed, 21 Sep 2022 12:59:25 +0500 Subject: [PATCH 03/13] Conflicts Resolve --- Task1.js | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/Task1.js b/Task1.js index 0ae7132..4fb4613 100644 --- a/Task1.js +++ b/Task1.js @@ -1,18 +1,27 @@ -function groupByKey(array, key) { - return array.reduce((hash, obj) => { - if(obj[key] === undefined) return hash; - return Object.assign(hash, { [obj[key]]:( hash[obj[key]] || [] ).concat(obj)}) - }, {}) -} +function GroupByKey(array,key) +{ + var tempData = []; + for ( var index=0; index Date: Wed, 21 Sep 2022 13:01:07 +0500 Subject: [PATCH 04/13] Conflicts Resolve --- Task1.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Task1.js b/Task1.js index 4fb4613..15d2c74 100644 --- a/Task1.js +++ b/Task1.js @@ -24,4 +24,4 @@ const array= [ let key='city'; -GroupByKey(array,key) \ No newline at end of file +console.log(GroupByKey(array,key)) \ No newline at end of file From 3e7d228ebd1ade57b5aba77179d385dffbaa1ada Mon Sep 17 00:00:00 2001 From: SaadOcloud Date: Wed, 21 Sep 2022 13:20:57 +0500 Subject: [PATCH 05/13] Conflicts Resolve --- Task1.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Task1.js b/Task1.js index 15d2c74..a39f99d 100644 --- a/Task1.js +++ b/Task1.js @@ -1,6 +1,6 @@ function GroupByKey(array,key) { - var tempData = []; + var tempData = {}; for ( var index=0; index Date: Wed, 21 Sep 2022 16:11:30 +0500 Subject: [PATCH 06/13] Resolve conflicts --- Task1.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Task1.js b/Task1.js index a39f99d..e398473 100644 --- a/Task1.js +++ b/Task1.js @@ -1,6 +1,6 @@ function GroupByKey(array,key) { - var tempData = {}; + tempData = {}; for ( var index=0; index Date: Wed, 21 Sep 2022 18:25:13 +0500 Subject: [PATCH 07/13] Task2 is completed with reduce function --- Task2.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Task2.js diff --git a/Task2.js b/Task2.js new file mode 100644 index 0000000..4a4efa8 --- /dev/null +++ b/Task2.js @@ -0,0 +1,24 @@ +const array=[ + {id:1,name:"Bilal", city:"Lahore"}, + {id:1,name:"Bilal", city:"Lahore"}, + {id:3,name:"Hafsa", city:"Karachi"}, + {id:4,name:"Rehan", city:"Lahore"}, + {id:5,name:"Saqib", city:"Karachi"}, + {id:6,name:"Farhan", city:"Islamabad"} +]; +const key='city'; + +function GroupByKey(arr,key) { + const tempData=arr.reduce((acc,cv)=>{ + if(!acc[cv[key]]){ + acc[cv[key]]=[] + } + acc[cv[key]].push(cv) + tempobj=acc + return acc + },{}); + return tempobj + +} + +console.log(GroupByKey(array,key)) From 97f9416b49f011697ca1bbebaf80a543de6c13b2 Mon Sep 17 00:00:00 2001 From: SaadOcloud Date: Fri, 23 Sep 2022 10:36:13 +0500 Subject: [PATCH 08/13] conflicts resolve --- Task1.js | 18 ++++++++++-------- Task2.js | 13 +++++++------ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Task1.js b/Task1.js index e398473..13014e6 100644 --- a/Task1.js +++ b/Task1.js @@ -1,15 +1,17 @@ function GroupByKey(array,key) { - tempData = {}; + const tempData = {}; - for ( var index=0; index{ - if(!acc[cv[key]]){ - acc[cv[key]]=[] + var tempobj=[]; + arr.reduce((accumulator,currentvalue)=>{ + if(!accumulator[currentvalue[key]]){ + accumulator[currentvalue[key]]=[] } - acc[cv[key]].push(cv) - tempobj=acc - return acc + accumulator[currentvalue[key]].push(currentvalue) + tempobj=accumulator + return accumulator },{}); return tempobj From 2f40d7c670566c6a209aa2576f5cc64fddc7b8b2 Mon Sep 17 00:00:00 2001 From: SaadOcloud Date: Mon, 26 Sep 2022 03:44:45 +0500 Subject: [PATCH 09/13] Task 3 --- Task3.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Task3.js diff --git a/Task3.js b/Task3.js new file mode 100644 index 0000000..0a01f0a --- /dev/null +++ b/Task3.js @@ -0,0 +1,28 @@ +const array=[ + {id:'one',next:"two", value:null}, + {id:'two',next:"three", value:null}, + {id:'three',next: null, value:null} +]; +var result=[] + +function updatearray(arr){ + for(var i=arr.length-1; i>-1;i--){ + updatevalue(arr[i]) + } +} + +function updatevalue(ele){ + if(ele.next==null){ + return (result.unshift(ele)) + } + else{ + for(var j in array){ + if(result[0].id==array[j].next){ + array[j].value=result[0] + return (result.unshift(array[j])) + } + } + } +} +updatearray(array) +console.log(result) \ No newline at end of file From bb78ab4990ccd0658434fdffc4c9fcceb914ddd6 Mon Sep 17 00:00:00 2001 From: SaadOcloud Date: Mon, 26 Sep 2022 11:53:44 +0500 Subject: [PATCH 10/13] Conflicts resolve --- Task3.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Task3.js b/Task3.js index 0a01f0a..968d5e8 100644 --- a/Task3.js +++ b/Task3.js @@ -19,10 +19,13 @@ function updatevalue(ele){ for(var j in array){ if(result[0].id==array[j].next){ array[j].value=result[0] - return (result.unshift(array[j])) + result.unshift(array[j]) + break } } } } updatearray(array) -console.log(result) \ No newline at end of file +result=JSON.stringify(result); +console.log(result) +// console.log(result[0].value) \ No newline at end of file From d06af609ba96d420090c6528a1d0632d5649c362 Mon Sep 17 00:00:00 2001 From: SaadOcloud Date: Mon, 26 Sep 2022 13:02:18 +0500 Subject: [PATCH 11/13] 2nd approach --- Task4.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Task4.js diff --git a/Task4.js b/Task4.js new file mode 100644 index 0000000..109f3f2 --- /dev/null +++ b/Task4.js @@ -0,0 +1,29 @@ +const array=[ + {id:'one',next:"two", value:null}, + {id:'two',next:"three", value:null}, + {id:'three',next: null, value:null} +]; +var result=[] +function updatevalue(array){ + if(result.length==0){ + for(var i in array){ + if(array[i].next==null){ + result.unshift(array[i]) + array.splice(array[i],1) + } + } + } + for(var j in array){ + if(result[0].id==array[j].next){ + array[j].value=result[0] + result.unshift(array[j]) + array.splice(array[j],1) + updatevalue(array) + break + } + } + +} +updatevalue(array.reverse()) + +console.log(result) \ No newline at end of file From 2dfb705bea363d17854fc9a5684a4f67298d2ce5 Mon Sep 17 00:00:00 2001 From: SaadOcloud Date: Mon, 26 Sep 2022 15:39:27 +0500 Subject: [PATCH 12/13] 3rd approach with recursion --- Task5.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Task5.js diff --git a/Task5.js b/Task5.js new file mode 100644 index 0000000..44a95ce --- /dev/null +++ b/Task5.js @@ -0,0 +1,23 @@ +const array=[ + {id:'one',next:"two", value:null}, + {id:'two',next:"three", value:null}, + {id:'three',next: null, value:null} +]; +var nextindex=0 +function updatearray(array){ + updatevalue(array,0) + function updatevalue(array,index){ + if(array[index].next==null){ + return array[index] + } + else{ + array[index].value=updatevalue(array,index+1) + return array[index] + } + } +} + +updatearray(array) +var arr=array +arr=JSON.stringify(array) +console.log(arr) \ No newline at end of file From 872153ba1051dc59f01667cc2f9c4b7dc0a1d0b8 Mon Sep 17 00:00:00 2001 From: SaadOcloud Date: Tue, 27 Sep 2022 19:43:14 +0500 Subject: [PATCH 13/13] Task 6 --- Task6.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Task6.js diff --git a/Task6.js b/Task6.js new file mode 100644 index 0000000..9878aa2 --- /dev/null +++ b/Task6.js @@ -0,0 +1,22 @@ +function delay(time) { + return new Promise((resolve, reject) => { + if (typeof(time) === 'string') { + console.log() + reject(new Error("Delay required a valid number")); + } + else{ + time *= 1000; + setTimeout(resolve, time); + } + }); +} + +async function main() { + await delay(2); + console.log("saad"); + await delay(2); + console.log("fareed"); + await delay(2); + console.log("happy Birthday"); +} +main();