Last active
July 10, 2023 10:54
-
-
Save Auwalms/9ff673329e5af137f877888ad8d3bd4a to your computer and use it in GitHub Desktop.
Tournament BYE Concept
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isPowerOf2(number) { | |
// If the number is 0, it is not a power of 2 | |
if (number === 0) { | |
return false; | |
} | |
// Check if the number is divisible by 2 without a remainder | |
while (number % 2 === 0) { | |
number = number >> 1; | |
} | |
// If the number is 1, it is a power of 2 | |
if (number === 1) { | |
return true; | |
} | |
// Otherwise, the number is not a power of 2 | |
return false; | |
} | |
function closestPowerOf2(number) { | |
// Find the next power of 2 greater than or equal to the number | |
var nextPowerOf2 = Math.pow(2, Math.ceil(Math.log2(number))); | |
// If the number is a power of 2, return the number | |
if (isPowerOf2(number)) { | |
return number; | |
} | |
// Otherwise, return the next power of 2 | |
return nextPowerOf2; | |
} | |
var number = 32; | |
console.log(`Is ${number} a power of 2: `, isPowerOf2(number)); // false | |
console.log(`${ closestPowerOf2(number)} is the closestPowerOf2` ); // 16 | |
let idealNumber = closestPowerOf2(number); | |
let providedNumber = isPowerOf2(number) | |
let numberOfBye = 0 | |
let arrayOfPlayers = [ | |
{ id: 1, name: "abc" }, | |
{ id: 2, name: "bcd", }, | |
{ id: 3, name: "bcd", }, | |
{ id: 4, name: "bcd", }, | |
{ id: 5, name: "bcd", }, | |
{ id: 6, name: "bcd", }, | |
{ id: 7, name: "bcd", }, | |
{ id: 8, name: "bcd", }, | |
{ id: 9, name: "bcd", }, | |
{ id: 10, name: "bcd", }, | |
{ id: 11, name: "bcd", }, | |
{ id: 12, name: "bcd", }, | |
{ id: 13, name: "bcd", }, | |
] | |
function createBYEs(numberOfBye) { | |
for (let index = 0; index < numberOfBye; index++) { | |
arrayOfPlayers.push({id:Math.random().toString(36).substr(2, length), name: "BYE"}) | |
} | |
return arrayOfPlayers | |
} | |
if (!providedNumber) { | |
numberOfBye = idealNumber - number | |
console.log(`${numberOfBye} is the number of BYE needed!`) | |
let allPlayers = createBYEs(numberOfBye) | |
console.log(allPlayers.length) | |
} | |
console.log("array of Players: ", arrayOfPlayers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment