Using toPrecision with map method

Multi tool use
Multi tool use

The name of the pictureThe name of the pictureThe name of the pictureClash 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;





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





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.

peEUPomQpxJC1R57R,ixKu2hUc0BeqedKmBXy0 l B 5iAHx1LZYe6h7ytJjSh2wP7otLgV2bcI8Z9Gg4uMxilz
fM1VPDBbTa jppLRU m 3 r8F2x0pwQsG

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3