Last active
April 12, 2018 11:51
-
-
Save colevandersWands/3c9f70848eac6ecb20fcda7d9b019877 to your computer and use it in GitHub Desktop.
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
// https://github.com/Mariana88/binary-addition | |
// her code for binary addition | |
// it's so long since she constrained herself to using only strings | |
// this makes binaryStrAddition long, | |
// she had to do logical comparisons between strings | |
// rather than simply pulling out elements | |
// it doesn't work with negative numbers, but that's not important for this execise | |
// we looked at how she split her primary strategy (binaryAddition) into two sub-procedures | |
// then we analyzed her strategy in convertToBinaryStr | |
// first while loop finds how many digits in the binary representaion (highest power) | |
// second loop builds up the binary representation | |
// as a little experiment we split this function into two functions | |
// one for each step in the above strategy | |
// we decided this was not a useful modularization | |
// converting to binary is probably more useful as a procedure in the future | |
// and both sub-tasks are not | |
function convertToBinaryStr (num){ | |
let hP = 0; | |
let numStr = ''; | |
let workNum; | |
if (num>=0){ | |
workNum = num; | |
} else { | |
workNum = -(num); | |
} | |
while (workNum - Math.pow(2, hP) >= 0){ | |
hP++; | |
} | |
hP--; | |
while (hP>=0){ | |
if (workNum - Math.pow(2, hP) >=0){ | |
numStr = numStr + 1; | |
workNum = workNum - Math.pow(2, hP); | |
} else { | |
numStr = numStr + 0; | |
} | |
hP--; | |
} | |
if (num <0){ | |
numStr = "-" + numStr; | |
} | |
return numStr; | |
} | |
function binaryStrAddition (binaryStr1, binaryStr2){ | |
let addedBinary = ""; | |
let extra = 0; | |
let i= binaryStr1.length - 1; | |
let j= binaryStr2.length -1; | |
while (i>=0 || j>=0){ | |
if ((binaryStr1[i] == 0 || binaryStr1[i] == undefined) && (binaryStr2[j]==0 || binaryStr2[j] == undefined) && extra==0){ | |
addedBinary = 0 + addedBinary; | |
extra = 0; | |
} else if ((((binaryStr1[i] == 1 && (binaryStr2[j]==0 || binaryStr2[j]==undefined))|| | |
((binaryStr1[i] == 0 || binaryStr1[i]==undefined) && binaryStr2[j]==1)) && extra == 0) || | |
((binaryStr1[i]==0 || binaryStr1[i]==undefined) && (binaryStr2[j]==0 || binaryStr2[j]==undefined) && extra==1)){ | |
addedBinary = 1 + addedBinary; | |
extra =0; | |
} else if (binaryStr1[i] == 1 && binaryStr2[j]==1 && extra == 1){ | |
addedBinary = 1 + addedBinary; | |
extra = 1; | |
} else { | |
addedBinary = 0 + addedBinary; | |
extra = 1; | |
} | |
i--; | |
j--; | |
} | |
if (extra == 1){ | |
addedBinary = 1 + addedBinary; | |
} | |
return addedBinary; | |
} | |
function binaryAddition (num1, num2){ | |
let myBinaryStr1 = convertToBinaryStr(num1); | |
let myBinaryStr2 = convertToBinaryStr(num2); | |
return binaryStrAddition (myBinaryStr1, myBinaryStr2); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment