From b42b72c4dbe110a766bdf8b6812ba2c75099c170 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Fri, 25 Jun 2021 03:24:27 +0200 Subject: [PATCH 1/2] refactor: added comments for understanding the question better around array being sorted and not allowing duplicates. Also fixed the solution --- answers/advanced/sharedNumbers-a.js | 37 +++++++++++++++-------------- questions/advanced/sharedNumbers.js | 1 + 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/answers/advanced/sharedNumbers-a.js b/answers/advanced/sharedNumbers-a.js index fe60ed7..b01af0a 100644 --- a/answers/advanced/sharedNumbers-a.js +++ b/answers/advanced/sharedNumbers-a.js @@ -1,25 +1,26 @@ function shared(x, y) { - const commonNumbers = []; - let curr1 = x.next(); - let curr2 = y.next(); + const commonNumbers = []; + let curr1 = x.next(); + let curr2 = y.next(); - while (curr1.done !== true && curr2.done !== true) { - if (curr1.value === curr2.value) { - commonNumbers.push(curr1.value); - curr1 = x.next(); - continue; - } + while (curr1.done !== true && curr2.done !== true) { + if (curr1.value === curr2.value) { + commonNumbers.push(curr1.value); + curr1 = x.next(); + curr2 = y.next(); + continue; + } - if (curr1.value > curr2.value) { - curr2 = y.next(); - continue; - } + if (curr1.value > curr2.value) { + curr2 = y.next(); + continue; + } - if (curr1.value < curr2.value) { - curr1 = x.next(); - continue; - } + if (curr1.value < curr2.value) { + curr1 = x.next(); + continue; } + } - return commonNumbers; + return commonNumbers; } diff --git a/questions/advanced/sharedNumbers.js b/questions/advanced/sharedNumbers.js index ab701b9..cdd3ffc 100644 --- a/questions/advanced/sharedNumbers.js +++ b/questions/advanced/sharedNumbers.js @@ -1,5 +1,6 @@ // Create a "shared" function that takes two array iterators // and returns the numbers common in both arrays. +// Assume the array is sorted in non decreasing order and has no duplicates /* const i1 = function* () { From b5ab8b24215dfa4244de983fba559fac783e8903 Mon Sep 17 00:00:00 2001 From: Vivek Nayyar Date: Fri, 25 Jun 2021 03:36:04 +0200 Subject: [PATCH 2/2] chore: changed from 2 to 4 spaces --- answers/advanced/sharedNumbers-a.js | 38 ++++++++++++++--------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/answers/advanced/sharedNumbers-a.js b/answers/advanced/sharedNumbers-a.js index b01af0a..0d8b890 100644 --- a/answers/advanced/sharedNumbers-a.js +++ b/answers/advanced/sharedNumbers-a.js @@ -1,26 +1,26 @@ function shared(x, y) { - const commonNumbers = []; - let curr1 = x.next(); - let curr2 = y.next(); + const commonNumbers = []; + let curr1 = x.next(); + let curr2 = y.next(); - while (curr1.done !== true && curr2.done !== true) { - if (curr1.value === curr2.value) { - commonNumbers.push(curr1.value); - curr1 = x.next(); - curr2 = y.next(); - continue; - } + while (curr1.done !== true && curr2.done !== true) { + if (curr1.value === curr2.value) { + commonNumbers.push(curr1.value); + curr1 = x.next(); + curr2 = y.next(); + continue; + } - if (curr1.value > curr2.value) { - curr2 = y.next(); - continue; - } + if (curr1.value > curr2.value) { + curr2 = y.next(); + continue; + } - if (curr1.value < curr2.value) { - curr1 = x.next(); - continue; + if (curr1.value < curr2.value) { + curr1 = x.next(); + continue; + } } - } - return commonNumbers; + return commonNumbers; }