LoginSignup
0
0

More than 3 years have passed since last update.

1からNまで足し算するシステムをつくってみた(自分用)

Last updated at Posted at 2019-09-19

システムを作ったきっかけ

JavaScriptの練習がてら作った、簡単なゲームの一つです。
自分の1作品としてこの場をお借りし、投稿したいと思います。

システム概要

画面に表示される小さな小窓に数値を入力すると、計算結果を出力します。

【図1】初期状態
Count1ToN.PNG

【図2】10を入力した結果
10result.PNG

また、小窓に1未満の数字や文字列を入力した場合は「1以上の数値を入力してください。」と出力されます。

コード

HTML

<!DOCTYPE HTML>
<html lang="ja">
  <head>
    <meta charset="UTF-8">
    <title>1からNまで足し算</title>
    <script type="text/javascript" src="script/sum.js"></script>
  </head>
  <body>
    <h2 id="text">1~Nの合計を計算します!</h2>
    <input id="max" value="数字を入力してください"/>
    <button onclick="calcSum()">ポチッ!</button>
  </body>
</html>

JavaScript

  function calcSum() {
    var max = document.getElementById("max").value;
    var total = 0;

    if(isNaN(max) === true || max < 1){
       document.getElementById("text").textContent = "1以上の数値を入力してください。";
       }
    else{
      var i = 1;
      while(i <= max){
        total += i;
        i++;
      }
      document.getElementById("text").textContent = "1から" + max + "までの合計は" + total + "です!";
    }
  }

あとがき

改善の余地があれば、ご指摘いただけたら幸いです。
また、他にもいろいろなシステムを作っているので、随時投稿させていただこうと思います。

0
0
2

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