Last active
October 19, 2016 17:24
-
-
Save LunaSquee/16ec2481869133d0f3d1543dbb380888 to your computer and use it in GitHub Desktop.
JavaScript CLI-style progress bar with variable width
This file contains hidden or 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
/** | |
* Example: | |
* progress_bar(40, 0.5); | |
* Returns: | |
* [####################-------------------] 50% | |
* | |
*/ | |
function progress_bar(barsize, percentage) { | |
let bar = ""; | |
let cnt = Math.floor(barsize * percentage); | |
for(let i=1;i<barsize;i++) { | |
if(i<=cnt) | |
bar += '#'; | |
else | |
bar += '-'; | |
} | |
return "["+bar+"] "+Math.floor(percentage * 100)+"%"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment