From 3a772f3516c8751ba951a1c6c4c4cce6c4f8ec67 Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Tue, 1 Aug 2023 19:50:15 +0100 Subject: add solutions week 228 in javascript --- challenge-228/steven-wilson/javascript/ch-01.js | 19 ++++++++++++++ challenge-228/steven-wilson/javascript/ch-02.js | 23 +++++++++++++++++ .../steven-wilson/javascript/test/index.html | 27 +++++++++++++++++++ .../steven-wilson/javascript/test/tests.js | 30 ++++++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 challenge-228/steven-wilson/javascript/ch-01.js create mode 100644 challenge-228/steven-wilson/javascript/ch-02.js create mode 100644 challenge-228/steven-wilson/javascript/test/index.html create mode 100644 challenge-228/steven-wilson/javascript/test/tests.js diff --git a/challenge-228/steven-wilson/javascript/ch-01.js b/challenge-228/steven-wilson/javascript/ch-01.js new file mode 100644 index 0000000000..0edb98ee7f --- /dev/null +++ b/challenge-228/steven-wilson/javascript/ch-01.js @@ -0,0 +1,19 @@ +"use strict"; + +/* Task 1: Unique Sum +You are given an array of integers. +Write a script to find out the sum of unique elements in the given array.*/ + +function sumUnique ( elements ) { + let elementCount = new Map(); + let sum = 0; + for (let elem of elements) { + elementCount.set(elem, (elementCount.get(elem) ?? 0 ) + 1); + } + for (let elem of elementCount.keys()){ + if( elementCount.get(elem) == 1){ + sum += elem; + } + } + return sum +} diff --git a/challenge-228/steven-wilson/javascript/ch-02.js b/challenge-228/steven-wilson/javascript/ch-02.js new file mode 100644 index 0000000000..044860dd49 --- /dev/null +++ b/challenge-228/steven-wilson/javascript/ch-02.js @@ -0,0 +1,23 @@ +"use strict"; + +/* Task 2: Empty Array +You are given an array of integers in which all elements are unique. +Write a script to perform the following operations until the array is +empty and return the total count of operations. +If the first element is the smallest then remove it otherwise move it to +the end. */ + +function emptyArray ( elements ) { + let operations = 0; + while (elements.length > 0){ + let elemSorted = elements.toSorted(); + if(elemSorted[0] == elements[0]){ + elements.shift(); + } + else { + elements.push(elements.shift()); + } + operations++; + } + return operations; +} diff --git a/challenge-228/steven-wilson/javascript/test/index.html b/challenge-228/steven-wilson/javascript/test/index.html new file mode 100644 index 0000000000..322f2615f3 --- /dev/null +++ b/challenge-228/steven-wilson/javascript/test/index.html @@ -0,0 +1,27 @@ + + + + + Mocha Tests + + + + + + + + + + + +
+ + + diff --git a/challenge-228/steven-wilson/javascript/test/tests.js b/challenge-228/steven-wilson/javascript/test/tests.js new file mode 100644 index 0000000000..21eddb69df --- /dev/null +++ b/challenge-228/steven-wilson/javascript/test/tests.js @@ -0,0 +1,30 @@ +describe("Challenge 228 task 1: sumUnique()", function() { + + it("2 unique elements", function() { + assert.equal( sumUnique([2, 1, 3, 2]), 4); + }); + + it("zero unique elements", function() { + assert.equal( sumUnique([1, 1, 1, 1]), 0); + }); + + it("all elements unique", function() { + assert.equal( sumUnique([2, 1, 3, 4]), 10); + }); +}); + + +describe("Challenge 228 task 2: emptyArray()", function() { + + it("array out of order", function() { + assert.equal( emptyArray([3, 4, 2]), 5); + }); + + it("array accending order", function() { + assert.equal( emptyArray([1, 2, 3]), 3); + }); + + it("array empty", function() { + assert.equal( emptyArray([]), 0); + }); +}); -- cgit