Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@ka25012

ヒストグラムの作成

今回はJavaScriptを用いて100人分のテストの点数をヒストグラムにして表すということをやっていきます

//以下のfor分で100人分のテストの結果を作成
//0.5.点以上100.5点未満の乱数を作成(Math.random()*100)+0.5
//これを切り下げる(Math.floor)ことで0以上100以下の結果が出る
// const scoreMake=[];
// for (i=0; i<100 ; i++){
//     score.push(Math.floor((Math.random()*100)+0.5))
// }
// console.log(scoreMake);

const score = [
  46,
  20,
  18,
  49,
  94,
  86,
  71,
  20,
  21,
  24,
  25,
  31,
  89,
  10,
  54,
  27,
  41,
  63,
  9,
  96,
  16,
  42,
  71,
  79,
  77,
  1,
  24,
  72,
  84,
  43,
  94,
  42,
  59,
  2,
  16,
  10,
  63,
  89,
  41,
  74,
  59,
  17,
  41,
  74,
  86,
  19,
  30,
  65,
  11,
  28,
  66,
  35,
  20,
  3,
  6,
  78,
  21,
  38,
  36,
  43,
  30,
  33,
  13,
  19,
  46,
  17,
  69,
  87,
  46,
  43,
  91,
  12,
  97,
  82,
  62,
  30,
  31,
  96,
  73,
  3,
  56,
  51,
  43,
  21,
  41,
  7,
  74,
  100,
  83,
  54,
  17,
  21,
  81,
  30,
  79,
  25,
  22,
  73,
  42,
  81,
];
// const count=[0点台,10点台,...,100点台]各要素10点ごとに何人いるか表すために作成
const count = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

for (i = 0; i < 100; i++) {
  //点数の10の位に注目して10の位だけを表す
  const num = Math.floor(score[i] / 10);
  //各要素に追加
  count[num]++;
}

// console.log(count)
// [(7, 13, 14, 10, 15, 6, 6, 12, 10, 6, 1)];

//何点台の人数が最も多いか取ってくる
const max = Math.max.apply(null,count);
//ヒストグラムの作成
for (i = 0; i < max; i++) {
    const array = [];
  for (j = 0; j < count.length; j++) {
    if (max - i <= count[j]) {
      array.push("*");
    } else {
      array.push(" ");
    }
  }
  array.join("")
  console.log(array.join("  "));
}
console.log("--------------------------------")
console.log("0 10 20 30 40 50 60 70 80 90 100")

//             *                  
//       *     *                  
//    *  *     *                  
//    *  *     *        *         
//    *  *     *        *         
//    *  *  *  *        *  *      
//    *  *  *  *        *  *      
//    *  *  *  *        *  *      
// *  *  *  *  *        *  *      
// *  *  *  *  *  *  *  *  *  *   
// *  *  *  *  *  *  *  *  *  *   
// *  *  *  *  *  *  *  *  *  *   
// *  *  *  *  *  *  *  *  *  *   
// *  *  *  *  *  *  *  *  *  *   
// *  *  *  *  *  *  *  *  *  *  *
// --------------------------------
// 0 10 20 30 40 50 60 70 80 90 100
0
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?