From 7a26178c15d9bf5006726a12a958a81c218dcd64 Mon Sep 17 00:00:00 2001 From: ricky Date: Wed, 17 Apr 2019 08:26:53 +0530 Subject: [PATCH] destructuring patterns --- destructuring/index.js | 98 ++++++++++++++++++++++++++++++++ {question.js => question}/lcs.js | 1 + question/queue.js | 36 ++++++++++++ templateString/index.js | 17 ++++++ 4 files changed, 152 insertions(+) create mode 100644 destructuring/index.js rename {question.js => question}/lcs.js (99%) create mode 100644 question/queue.js create mode 100644 templateString/index.js diff --git a/destructuring/index.js b/destructuring/index.js new file mode 100644 index 0000000..0bc6a69 --- /dev/null +++ b/destructuring/index.js @@ -0,0 +1,98 @@ +function getData() { + return [ + { + name: "kyle", + email: "kyle@gmail.com", + full_name: "hesam sameni", + avatar: "https://aaa/file/image/hesamsameni.jpeg", + rate: "0.0", + phone: [], + location: { + name: "something", + lat: 00, + long: 00, + address: "something", + distance: 00 + }, + about: { + quote: "something", + bio: "something" + }, + topics: { + a: "a", + b: "b", + c: "c", + d: "d" + }, + stats: { + a: 10, + b: 0, + c: 0, + d: 0, + e: 0 + } + }, + { + name: "simpson", + email: "simpson@gmail.com", + full_name: "hesam sameni", + avatar: "https://aaa/file/image/hesamsameni.jpeg", + rate: "0.0", + phone: [], + location: { + name: "something", + lat: 00, + long: 00, + address: "something", + distance: 00 + }, + about: { + quote: "something", + bio: "something" + }, + topics: { + a: "a", + b: "b", + c: "c", + d: "d" + }, + stats: { + a: 10, + b: 0, + c: 0, + d: 0, + e: 0 + } + } + ]; +} + +(function() { + const [ + { name: firstName, email: firstEmail }, + { name: secondName, email: secondEmail = "getify@gmail.com" } + ] = getData(); + + console.log( + `firstEmail : ${firstEmail} + firstName: ${firstName} + secondEmail:${secondEmail} + secondName: ${secondName}` + ); +})(); + +function initializeMultiple() { + return [1, 2, 3]; +} + +// handling undefined and default value +(function() { + const [first, second, third, forth = 4] = initializeMultiple() || []; + console.log(first, second, third, forth); +})(); + +// gathering rest of values after destructuring in a variable +(function() { + const [one, two, ...rest] = initializeMultiple(); + console.log(one, two, rest); +})(); diff --git a/question.js/lcs.js b/question/lcs.js similarity index 99% rename from question.js/lcs.js rename to question/lcs.js index e3099cd..513e9e4 100644 --- a/question.js/lcs.js +++ b/question/lcs.js @@ -32,6 +32,7 @@ console.log( sl2: string2.length - 1 }) ); + function LCS(S, n, T, m) { if (n === -1 || m === -1) return 0; let result; diff --git a/question/queue.js b/question/queue.js new file mode 100644 index 0000000..ce2e9b4 --- /dev/null +++ b/question/queue.js @@ -0,0 +1,36 @@ +a = [1, 2, 3, 4, 5, 6]; + +class queue { + constructor(initialData = []) { + this.queue = initialData; + } + + enqueue(x) { + this.queue[this.queue.length] = x; + return { success: true }; + } + + dequeue() { + if (this.queue.length) { + const [firstElement, ...restOfArray] = this.queue; + this.queue = restOfArray; + return firstElement; + } + } +} + +// let newQueue = new queue(a); +// console.log(newQueue.queue); +// console.log(newQueue.enqueue(11)); +// console.log(newQueue.dequeue()); + +let newQueue2 = new queue(); +console.log(newQueue2.queue); +console.log(newQueue2.enqueue(11)); +console.log(newQueue2.enqueue(12)); + +console.log(newQueue2.enqueue(13)); +console.log(newQueue2.queue); + +console.log(newQueue2.dequeue()); +console.log(newQueue2.queue); diff --git a/templateString/index.js b/templateString/index.js new file mode 100644 index 0000000..47bb435 --- /dev/null +++ b/templateString/index.js @@ -0,0 +1,17 @@ +function upper(strings, ...values) { + let outputString = ""; + for (i = 0; i < values.length; i++) { + outputString += `${strings[i]}${values[i].toUpperCase()}`; + } + outputString += `${strings[i]}`; + return outputString; +} + +var name = "kyle", + twitter = "getify", + topic = "JS Recent Parts"; + +console.log( + upper`Hello ${name} (@${twitter}), welcome to ${topic}!` === + "Hello KYLE (@GETIFY), welcome to JS RECENT PARTS!" +);