Edabit

Build a function that creates histograms. Every bar needs to be on a new line and its length corresponds to the numbers in the array passed as an argument. The second argument of the function represents the character to be used for the bar.

 histogram(arr, char) ➞ str 

Examples

 histogram([1, 3, 4], "#") ➞ "#\n###\n####"  # ### ####  histogram([6, 2, 15, 3], "=") ➞ "======\n==\n===============\n==="  ====== == =============== ===  histogram([1, 10], "+") ➞ "+\n++++++++++"  + ++++++++++ 

Notes

For better understanding try printing out the result.

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16
//1st solution function histogram(arr, char) {   let sol = [];   for (let num of arr) {     let word = Array.from({ length: num }, () => char);     sol.push(word.join(""));   }   return sol.join("\n"); }  //2nd solution function histogram2(arr, char) {   return arr     .map((num) => Array.from({ length: num }, () => char).join(""))     .join("\n"); } 

This free site is ad-supported. Learn more