0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

javascript 2つの配列の平均値

Last updated at Posted at 2020-08-18

複数の数値が定義されている配列data1とdata2から「data1とdata2を合わせた平均値」と「平均値以上の数値」を表示するプログラムを作成する。


<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>課題</title>
    <script>
        var data1 = [59, 39, 100, 2, 15, 40, 84, 97];
        var data2 = [63, 18, 64, 97, 50, 98];
        var sum = 0;
        
        for(var i = 0; i < data1.length; i++) {
            sum += data1[i];
        }
        for(var j = 0; j < data2.length; j++) {
            sum += data2[j];
        }
        var ave = sum/(data1.length+data2.length);
        document.write('<p>data1とdata2を合わせた平均値:'+ave+'</p>');
        document.write('<p>平均値以上の数値は以下</p>')
        
        for(var i = 0; i < data1.length; i++) {
         if(data1[i]>=ave) {
            document.write('<p>data1:'+data1[i]+'</p>');
          }
        }
        for(var j=0; j<data2.length; j++){
         if(data2[j]>=ave) {
            document.write('<p>data2:'+data2[j]+'</p>');
         }
        }
        
    </script>
</head>
<body>
</body>
</html>

初めて学習したときはこれでもかなり苦戦ししました。for文とif文をある程度理解していればかんたんになりますねぇ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?