Using toPrecision with map method
Clash Royale CLAN TAG#URR8PPP
Using toPrecision with map method
I have a function that returns long numbers. 23.23423423423432
I can strip the decimal places:
function shortNumbers()
var longNumber = 34.324234234234;
var shortNumber = longNumber.toPrecision(2)
alert("Shorter number is " + shortNumber)
Next I want to use map to display the values in an alert:
function collectVideoValues()
var loopsStr = loops.map(x=>x.start+"AA"+x.end).join('AA');
alert("Video Values are " + videoId + "," +loopsStr );
Can I insert toPrecision after loops.map ?
loops.map.toPrecision(2).(x=>x.start+"AA"+x.end).join('AA');
I'd like to solve this in 1 or 2 lines of code without making a second function.
1 Answer
1
I placed toPrecision after x.start and x.end to strip the decimal places from the number.
function collectVideoValues()
var loopsStr = loops.map(x=>x.start.toPrecision(2)+"i"+x.end.toPrecision(2)).join('i');
document.getElementById("lesson-code").innerHTML= videoId + "ID"+ loopsStr;
Explanation added!
– RubyRube
Aug 9 at 2:15
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
– Narendra Jadhav
Aug 8 at 5:46