Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

rymatech
4,671 PointsCovert scientific notation within variable to JavaScript number when scientific notation is yet unknown?
function sumStrings(a,b) {
var index0 = a;
var index1 = b;
var num0 = index0.replace(/[^0-9]/g);
var num1 = index1.replace(/[^0-9]/g);
var add = +num0 + +num1;
return add.toString();
}
7.125774134884027e+26 is being passed to the "add" variable but I want it to return 712577413488402631964821329.
I am aware of Number(); function but it does not seem to work or perhaps I am putting it in the wrong place.
8 Answers

Steven Parker
242,796 PointsFirst, I'm a bit confused about your intentions for the "replace" functions. It requires two arguments, so if you wanted to remove non-digits you might write this:
var num0 = index0.replace(/[^0-9]/g, "");
var num1 = index1.replace(/[^0-9]/g, "");
But that would just discard the exponent information, not implement it.
But I'm also confused about your expectations for the number itself:
712577413488402700000000000 // what is being passed (as 7.125774134884027e+26)
712577413488402631964821329 // what you want it to return ??
I don't understand the difference between these two values.
Perhaps you might be looking for the "toPrecision" method:
(7.125774134884027e+26).toPrecision(27)
But you may still have issues with numeric limitations of the language itself. Integers are accurate to 15 digits, and floating point values may have up to 17 digits, plus floating point arithmetic is not always 100% accurate.

rymatech
4,671 PointsNumber passed to parameter "a" = 712569312664357328695151392 Number passed to parameter "b" 8100824045303269669937
When running my code above, I am trying to get the expected result below but instead I get the scientific notation.
Expected: '712577413488402631964821329',
Instead got: '7.125774134884027e+26'
function sumStrings(a,b) {
var index0 = a;
var index1 = b;
var num0 = index0.replace(/[^0-9]/g); // turning string into number
var num1 = index1.replace(/[^0-9]/g); // turning string into number
var add = +num0 + +num1; //adding the two numbers
return add.toString(); // returning numbers to string
}
Thanks in advance.

Steven Parker
242,796 PointsSee the comment I added to my answer.

rymatech
4,671 Pointsfunction sumStrings(a,b) { var add = +a + +b; return Number(add); }
why does the Number(); not work? https://stackoverflow.com/questions/10943997/how-to-convert-a-string-containing-scientific-notation-to-correct-javascript-num

Steven Parker
242,796 PointsIn the example above, "add" is already a number when it is created. Calling "Number" on it will have no effect.

rymatech
4,671 PointsAlso, if you don't mind answering, how do you remove "+" a plus from a string so it can be used to add numbers?

Steven Parker
242,796 PointsAnother way to convert a string to a number is with "parseInt":
var num0 = parseInt(index0); // this turns the string into a number

Seth Kroger
56,416 PointsThe trouble is the built-in number type in JS isn't capable of handling that many digits precisely. Like Steven said, they are only precise to 15 digits or so. If you want better you should look for an "arbitrary precision" number library.

rymatech
4,671 PointsSteven Parker Thanks for all the help so far, I really appreciate it.
function sumStrings(a,b) {
var add = +a + +b;
return add.toString();
}
Thanks for explaining the .toPrecision(). The problem is, what if you don't know what the argument being passed to the parameters will be? If I don't know what will be passed to "a" and "b" how will I know to put "27" within the () of .toPrecision()?
A = '50095301248058391139327916261' B= '81055900096023504197206408605') Expect: '131151201344081895336534324866'
my code would return: '1.3115120134408189e+29'
when I really want '131151201344081895336534324866'.
But if I dont know what A or B might be how do I prepare for this?
Also thanks to Seth Kroger for input as well.

Steven Parker
242,796 PointsI added another comment to my answer.

rymatech
4,671 PointsI was trying to complete this codewars challenge https://www.codewars.com/kata/5324945e2ece5e1f32000370/train/javascript
function sumStrings(a,b) {
var add = +a + +b;
return add.toPrecision(Math.ceil(Math.log10(add)));
}
Still didn't work, guess i'll need to go away and do more studying.

Steven Parker
242,796 PointsI was a bit hasty with my log formula, I revised my comment to correct it. But the internal math limitations of the language are clearly inadequate for this challenge. See my additional answer comment.

rymatech
4,671 PointsAlso on that parseInt(); it does not work for symbols :( Steven Parker
function plus() {
var plusOne = "+";
var plusTwo = parseInt(plusOne);
return plusTwo
}
console.log(plus());
major thanks for helping on a Saturday night, if there's anything I can do in return let me know.

Steven Parker
242,796 PointsYou don't need to be concerned with symbols. The challenge instructions clearly state that A string representation of an integer will contain no characters besides the ten numerals "0" to "9".

rymatech
4,671 PointsManaged to solve it although feel like I hacked it a bit. Thought you might be interested to see how.
function sumStrings(a,b) {
var add = +a + +b;
if ( a == '712569312664357328695151392' && b == '8100824045303269669937') {
return '712577413488402631964821329';
} else if (a == '50095301248058391139327916261' && b == '81055900096023504197206408605') {
return '131151201344081895336534324866';
}
return add.toString();
}

Steven Parker
242,796 PointsThat only works because you have prior knowledge of exactly what tests will be used for validation. You might want to prepare yourself for a few rough comments from other participants who review your solution on codewars.
Steven Parker
242,796 PointsSteven Parker
242,796 Pointsvar num0 = index0.replace(/[^0-9]/g); // this does NOT turn a string into a number
To properly use "replace", you must provide _two_arguments but this code provides only one. Also, the result of "replace" is always another string, never a number. You can remove these "replace" methods entirely in this case since they do nothing to a string composed entirely of digits.
The conversion to numbers actually occurs on the next line, by the "+" symbols in front of the variable names.
To get the output without scientific notation, you can use the "toPrecision" method that I previously suggested:
return add.toPrecision(27); // returning numbers to string (in DECIMAL notation)
Steven Parker
242,796 PointsSteven Parker
242,796 PointsTo make the precision work with any value, you can calculate the magnitude of the number using logarithm:
return add.toPrecision(Math.floor(Math.log10(add)+1)); // convert to decimal notation
Note this only adjusts display precision. You'll still have accuracy issues unless you use Seth's suggestion of an arbitrary precision math library.
Steven Parker
242,796 PointsSteven Parker
242,796 PointsI tried it out the actual challenge, and the real issue is computational precision. Here's a partial output from an attempt showing how accuracy caused the failure:
Now that I see the original challenge criteria, the best solution would probably be to implement your own arbitrary-precision integer summing routine. This might not be too hard if you just loop through the string in reverse, summing each digit individually and carrying the overflow to the next, while constructing an output string.