Skip to content

Instantly share code, notes, and snippets.

@alexcrist
Created March 4, 2021 18:01
Show Gist options
  • Save alexcrist/a4ebde3e6bdb96e5339f70e89d01b9d1 to your computer and use it in GitHub Desktop.
Save alexcrist/a4ebde3e6bdb96e5339f70e89d01b9d1 to your computer and use it in GitHub Desktop.
function foldTo(distance) {
// If we're given a bogus distance, return null
if (distance < 0) {
return null;
}
// Initial paper thickness and number of folds
let paperThickness = 0.0001;
let numFolds = 0;
// Fold our paper until it is equal to or greater than the given distance
while (paperThickness < distance) {
paperThickness = paperThickness * 2;
numFolds = numFolds + 1;
}
// Output numFolds
return numFolds;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment