diff options
| -rw-r--r-- | challenge-228/steven-wilson/javascript/ch-01.js | 19 | ||||
| -rw-r--r-- | challenge-228/steven-wilson/javascript/ch-02.js | 23 | ||||
| -rw-r--r-- | challenge-228/steven-wilson/javascript/test/index.html | 27 | ||||
| -rw-r--r-- | challenge-228/steven-wilson/javascript/test/tests.js | 30 |
4 files changed, 99 insertions, 0 deletions
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 @@ +<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="utf-8" /> + <title>Mocha Tests</title> + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> + <link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" /> + <script src="https://unpkg.com/mocha/mocha.js"></script> + <script class="mocha-init"> + mocha.setup('bdd'); + </script> + <script src="https://unpkg.com/chai/chai.js"></script> + <script> + // make assert global + let assert = chai.assert; + </script> + </head> + <body> + <script src="../ch-01.js"></script> + <script src="../ch-02.js"></script> + <script src="tests.js"></script> + <div id="mocha"></div> + <script class="mocha-exec"> + mocha.run(); + </script> + </body> +</html> 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); + }); +}); |
