Created
July 14, 2018 01:05
-
-
Save ambergkim/054f3c2b752623ffb2992a58b420135e to your computer and use it in GitHub Desktop.
JavaScript division with division operator
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
// Input: int, divInt | |
// Output: int | |
// helper | |
// return whole sets & remainder | |
// input: int, divInt | |
// output: [sets, remainder] | |
// main | |
// fin str | |
// whole sets | |
// remainder | |
// let res1 = helper(); | |
// whole sets = res[0] | |
// remainder = res2[1] | |
// fin str + whole sets + '.' | |
// while remainder | |
// let res2 = helper(rem * 10) | |
// whole sets = res2[0] | |
// remainder = res2[1] | |
// fin str + wholesets | |
// return parse int finstr | |
function divHelper(int, divInt) { | |
let sets = 0; | |
let count = 0; | |
for (let i = 0; i < int; i++) { | |
if (count < divInt) { | |
count++; | |
} | |
if (count === divInt) { | |
sets++; | |
count = 0; | |
} | |
} | |
return [sets, count]; | |
} | |
function div(int, divInt) { | |
let finStr = ''; | |
let sets = 0; | |
let remainder = 0; | |
let wholeRes = divHelper(int, divInt); | |
sets = wholeRes[0]; | |
remainder = wholeRes[1]; | |
finStr = finStr + sets + '.'; | |
while (remainder && (finStr.length < 5)) { | |
let subRes = divHelper(remainder * 10, divInt) { | |
sets = subRes[0]; | |
remainder = subRes[1]; | |
finStr = finStr + sets; | |
} | |
} | |
return parseInt(finStr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment