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?

7/13 本日の学習内容

Posted at

JavaScript/HTML/CSSを使用した簡単な電卓を作ろう

模写したコード(JS編)

function get_calc(btn) {

  // ”=”を押した時の挙動
  //
  if (btn.value == '=') {
    document.dentaku.display.value = eval(document.dentaku.display.value);
  } else if (btn.value == 'C') {
    document.dentaku.display.value = "";
  } else {
    if (btn.value == '×') {
      btn.value = '*';
    } else if (btn.value == '÷') {
      btn.value = '/';
    }

    // 
    document.dentaku.display.value += btn.value;

    // ボタンの見た目を変化させる
    document.dentaku.multi_btn.value = '×';
    document.dentaku.div_btn.value = '÷';
  }
}

出力結果

2025-07-13 19.43の画像.jpeg

新しく覚えたこと

eval()

文字列として渡された式を、JavaScriptのコードとして実行する関数。危険らしい?

2025-07-13 21.26の画像.jpeg

次は実際に自分で電卓をコーディングします

0
0
1

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?