From ae15bcef583c97663976e933224adadb5317eea5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CSamuelAjalode=E2=80=9D?= Date: Wed, 14 Sep 2022 15:05:58 +0100 Subject: [PATCH 1/3] my mini-challenges-3 solution --- src/binary-reversal/index.js | 13 ++++++++++++- src/list-sorting/index.js | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/binary-reversal/index.js b/src/binary-reversal/index.js index 965bccf4..c7067784 100644 --- a/src/binary-reversal/index.js +++ b/src/binary-reversal/index.js @@ -3,6 +3,17 @@ * * * @param {string} value */ -function binaryReversal(value) {} + function binaryReversal(value) { + + let t = value.toString(2).split(""); + let str_len = t.length; + for (let i = 0; i < 8 - str_len; i++) { + t.unshift("0"); + } + return parseInt(t.reverse().join(""), 2); +} +// 14 -> 00001110 -> 01110000 -> 112 +console.log(binaryReversal(121)); + module.exports = binaryReversal; diff --git a/src/list-sorting/index.js b/src/list-sorting/index.js index 6636c20d..eef39887 100644 --- a/src/list-sorting/index.js +++ b/src/list-sorting/index.js @@ -1,3 +1,31 @@ -function listSorting(needle, haystack) {} +/** + * @param {string} haystack + * @param {string} needle + * @return {number} + */ + function listSorting(haystack, needle) { + if (!needle.length) return 0; -module.exports = listSorting; + // Loop through the haystack's letters + for (let i = 0; i <= haystack.length - needle.length; i++) { + // Check if the current letter matches the start of the needle + if (haystack[i] === needle[0]) { + // Loop through the needle + for (let j = 0; ; j++) { + // Reached the end of the needle (and thus fully found it at i) + if (j == needle.length) { + return i; + } + // Letters not matched (needle not found at i) + else if (haystack[i + j] !== needle[j]) { + break; + } + } + } + } + return haystack; +} + +console.log(listSorting(5, [1, 2, 3, 4, 5])); + +module.exports = listSorting; \ No newline at end of file From e949bf2712697400306252f9c5ce3e2353452bf5 Mon Sep 17 00:00:00 2001 From: samuelajalode <113217825+samuelajalode@users.noreply.github.com> Date: Fri, 11 Nov 2022 10:15:46 +0100 Subject: [PATCH 2/3] Update test.js --- src/binary-reversal/test.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/binary-reversal/test.js b/src/binary-reversal/test.js index 5ea0811d..cde33551 100644 --- a/src/binary-reversal/test.js +++ b/src/binary-reversal/test.js @@ -1,13 +1,15 @@ -const binaryReversal = require("."); +function binaryReversal (str) { + // code goes here + let num = parseInt(str); + let binary = num.toString(2); + let reverse = binary.split("").reverse().join(""); + let newNum = parseInt(reverse, 2); + let final = newNum.toString(10); + return final; + } -describe("Binary Reversal Spec", () => { - test("It passes the basics", () => { - expect(binaryReversal("1")).toBe("128"); - expect(binaryReversal("10")).toBe("80"); - expect(binaryReversal("45")).toBe("180"); - expect(binaryReversal("100")).toBe("38"); - expect(binaryReversal("111")).toBe("246"); - expect(binaryReversal("121")).toBe("158"); - expect(binaryReversal("200")).toBe("19"); - }); -}); + console.log(binaryReversal("47")); + console.log(binaryReversal("213")); + console.log(binaryReversal("4567")); + + module.exports = binaryReversal; From 90275a9fe4fb7f63128540b79a595cf4b5185172 Mon Sep 17 00:00:00 2001 From: samuelajalode <113217825+samuelajalode@users.noreply.github.com> Date: Fri, 11 Nov 2022 10:25:47 +0100 Subject: [PATCH 3/3] Update index.js --- src/list-sorting/index.js | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/src/list-sorting/index.js b/src/list-sorting/index.js index eef39887..2b984e04 100644 --- a/src/list-sorting/index.js +++ b/src/list-sorting/index.js @@ -1,31 +1,17 @@ -/** + /** * @param {string} haystack * @param {string} needle * @return {number} */ function listSorting(haystack, needle) { - if (!needle.length) return 0; + // code goes here + let arr = haystack.split(","); + let sorted = arr.sort(); + let index = sorted.indexOf(needle); + return index; + } - // Loop through the haystack's letters - for (let i = 0; i <= haystack.length - needle.length; i++) { - // Check if the current letter matches the start of the needle - if (haystack[i] === needle[0]) { - // Loop through the needle - for (let j = 0; ; j++) { - // Reached the end of the needle (and thus fully found it at i) - if (j == needle.length) { - return i; - } - // Letters not matched (needle not found at i) - else if (haystack[i + j] !== needle[j]) { - break; - } - } - } - } - return haystack; -} + console.log(listSorting("hello,world,here,i,am", "world")); -console.log(listSorting(5, [1, 2, 3, 4, 5])); -module.exports = listSorting; \ No newline at end of file +module.exports = listSorting;