diff --git a/src/binary-reversal/index.js b/src/binary-reversal/index.js index 965bccf4..809f4e31 100644 --- a/src/binary-reversal/index.js +++ b/src/binary-reversal/index.js @@ -3,6 +3,18 @@ * * * @param {string} value */ -function binaryReversal(value) {} - -module.exports = binaryReversal; + function binaryReversal(value) { + // conversion of integar to binary + let toBinary = parseInt(value).toString(2); + // padding of the binary to 8 bits. + let padBinary = toBinary.padStart(8,0); + // split the padded binary,reverse and join. + let reversedBinary = padBinary.split('').reverse().join('') + // convert the reversed binary into digits and store in a variable finalNum + let finalNum = parseInt(reversedBinary, 2); + // return the final result + return `${finalNum}` + + } + module.exports = binaryReversal; + \ No newline at end of file diff --git a/src/list-sorting/index.js b/src/list-sorting/index.js index 6636c20d..2b49ee07 100644 --- a/src/list-sorting/index.js +++ b/src/list-sorting/index.js @@ -1,3 +1,19 @@ -function listSorting(needle, haystack) {} - -module.exports = listSorting; +function listSorting(needle, haystack) { + // if haystack is not a single array, return last index of haystack(needle). + if(!Array.isArray(haystack[0])){ + return haystack.lastIndexOf(needle) + } + // loop through the haystack row using for loop in descending order + // loop through the haystack column to identify the last index(needle) + // if the last index of the colum exists, return row, column, else return -1. + for (let row = haystack.length-1; row >= 0; row--){ + let col = haystack[row].lastIndexOf(needle) + if (col !==-1){ + return [row, col] + } + } + return -1 + } + + module.exports = listSorting; +