Javascript: Vertical histogram

JS: Functions
Last update: 13 Sep 23:18
0
Students

Dice is a six-sided die that is thrown several times. A histogram is a graphical representation of data in the form of columns or columns.

histogram.js

Implement and export as default a function that displays a vertical histogram. The function takes as input the number of dice rolls and a function that simulates the dice roll (already implemented). The output of this function is a number between 1 and 6 that represents one of the six faces of the dice.

The dice faces and the number of their rolls are represented by columns in the histogram. The results are displayed graphically (using the # characters) and as a percentage of the total number of rolls, except when their number is 0 (zero).

Additional conditions:

  • Percentage values should be left aligned relative to the column
  • The values of the dice faces should be in the middle of the column
  • Columns are separated by a space
  • The number of sections in the column (column height) must correspond to the number of falls of each side of the dice

Examples

import displayHistogram from '../histogram.js';

displayHistogram(32, rollDie);
// =>                 28%
//                    ###
//                    ###
//            19%     ###
//            ### 16% ### 16%
//    13%     ### ### ### ###
//    ### 9%  ### ### ### ###
//    ### ### ### ### ### ###
//    ### ### ### ### ### ###
//    ### ### ### ### ### ###
//    -----------------------
//     1   2   3   4   5   6

displayHistogram(13, rollDie);
// =>                 31% 31%
//                    ### ###
//        15%     15% ### ###
//        ### 8%  ### ### ###
//        ### ### ### ### ###
//    -----------------------
//     1   2   3   4   5   6

Tips

  • Histogram
  • Use the functions from the lodash library to solve the task
  • When getting a percentage value, use the standard rules for rounding numbers

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests