SHARE
TWEET

04. Histogram - for - Loops

vborislavova Feb 24th, 2020 (edited) 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function histogram(input) {
  2.     let numbers = Number(input.shift());
  3.  
  4.     let p1 = 0;
  5.     let p2 = 0;
  6.     let p3 = 0;
  7.     let p4 = 0;
  8.     let p5 = 0;
  9.  
  10.     for (let n = 1; n <= numbers; n++) {
  11.        
  12.       let currentNumber = Number(input.shift());
  13.  
  14.         if(currentNumber < 200) {
  15.             p1++;
  16.         } else if (currentNumber >= 200 && currentNumber <= 399) {
  17.             p2++;
  18.         } else if (currentNumber >= 400 && currentNumber <= 599) {
  19.             p3++;
  20.         } else if (currentNumber >= 600 && currentNumber <= 799) {
  21.             p4++;
  22.         } else {
  23.             p5++
  24.         }
  25.     }
  26.  
  27.     let p1Percent = p1 / numbers * 100;
  28.     let p2Percent = p2 / numbers * 100;
  29.     let p3Percent = p3 / numbers * 100;
  30.     let p4Percent = p4 / numbers * 100;
  31.     let p5Percent = p5 / numbers * 100;
  32.  
  33.     console.log(`${p1Percent.toFixed(2)}%`);
  34.     console.log(`${p2Percent.toFixed(2)}%`);
  35.     console.log(`${p3Percent.toFixed(2)}%`);
  36.     console.log(`${p4Percent.toFixed(2)}%`);
  37.     console.log(`${p5Percent.toFixed(2)}%`);
  38.  
  39. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top