Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Farooq Olaide Ajibade mini-challenges-3 #147

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/binary-reversal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,41 @@
*
* * @param {string} value
*/
function binaryReversal(value) {}
function binaryReversal(value) {
let loop = Math.floor(value**0.5) + 1
let arr = []
for (let i = 0; i < loop; i++) {

if (value > 1) {
arr.push(value % 2)
value = Math.floor(value/2)
} else if (value == 1) {
arr.push(1)
value = 0
}
}
console.log(arr);
let strNum = arr.join('')
let length = strNum.length

let n8 = Math.ceil(length/8)

if (length < (8 * n8)) {
let diff = (8 * n8) - strNum.length;

for (let i = 0; i < diff; i++) {
strNum += '0'
}
}
let newLength = strNum.length

let sumArray = []
for (let i = 0; i < newLength; i++) {
let pow = newLength - 1 - i
let num = strNum[i]
sumArray.push(num * (2**pow))
}
return sumArray.reduce((a, b) => a + b).toString();
}

module.exports = binaryReversal;
38 changes: 37 additions & 1 deletion src/list-sorting/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
function listSorting(needle, haystack) {}
function listSorting(needle, haystack) {
if (haystack.length == 0) return -1

let arr = [];

if (typeof(haystack[0]) == 'object') {

for (let i = 0; i < haystack.length; i++) {
if(haystack[i].length != 0 && haystack[i].lastIndexOf(needle) != -1) {
arr.push(haystack[i].lastIndexOf(needle))
} else {
arr.push(null)
}
}

arr
if (arr.every((el) => el == null)) return -1

let arr2 = []

for (let i = 0; i < arr.length; i++) {
let arr1 = []
if (typeof(arr[i]) == 'number') {
arr1.push(arr.lastIndexOf(arr[i]), arr[i])
} else continue
arr2.push(arr1)
}

arr2
if (arr2.length == 0) return -1
return arr2[arr2.length-1]
} else if (typeof(haystack[0]) == 'number') {
return haystack.lastIndexOf(needle)
}
}



module.exports = listSorting;