diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..6c2ff60b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "githubPullRequests.ignoredPullRequestBranches": [ + "master" + ] +} \ No newline at end of file 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