Created
July 14, 2018 00:49
-
-
Save ambergkim/87ff6f909e4f306a766ca284b447b13f to your computer and use it in GitHub Desktop.
Create a function to divide integers w/o using javascript division.
This file contains 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
// Inputs: int, divideByInt | |
// Output: Int | |
// Sets | |
// Current Set | |
// Loop through int | |
// if current < divide | |
// current ++ | |
// if current = divide | |
// sets ++ | |
// current set = 0 | |
// if current set > 0 | |
// current set * 10 | |
// divide again | |
// return parseInt sets + . + sets2 | |
// ex 11 / 2 | |
// sets 5 | |
// remainder 1 * 10 = 10/2 = 5. | |
// ex 13 / 5 | |
// sets 2 | |
// remainder 3 * 10 = 30/5 = 6 | |
// 2.6 | |
function divide(int, divInt) { | |
let sets = 0; | |
let currentS = 0; | |
let decimal = 0 | |
let decimalSets = 0; | |
let decimalCurS = 0; | |
for (let i = 0; i < int; i++) { | |
if (currentS < divInt) { | |
currentS++; | |
} | |
if (currentS === divInt) { | |
sets++; | |
currentS = 0; | |
} | |
} | |
if (currentS > 0) { | |
let decimal = currentS * 10; | |
for (let i = 0; i < decimal; i++) { | |
if (decimalCurS < divInt) { | |
decimalSets++; | |
decimalCurrS = 0; | |
} | |
} | |
} | |
return parseInt(sets + '.' + decimalSets); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment