Skip to content

Instantly share code, notes, and snippets.

@Spuffynism
Created May 28, 2020 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Spuffynism/17f2317398b54b2df9213e7a30f4842d to your computer and use it in GitHub Desktop.
Save Spuffynism/17f2317398b54b2df9213e7a30f4842d to your computer and use it in GitHub Desktop.
Print a nice graph
const bars = [1, 2, 4, 1, 4, 7, 2, 12, 12, 5];
const heightOfGraph = Math.max(...bars);
let graph = '';
for (let i = heightOfGraph; i > 0; i--) {
graph += bars.reduce((row, bar) => row + (i - bar > 0 ? ' ' : '+'), '') + '\n';
}
console.log(graph);
/*
++
++
++
++
++
+ ++
+ ++
+ +++
+ ++ +++
+ ++ +++
++ ++++++
++++++++++
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment