LoginSignup
2
2

More than 1 year has passed since last update.

文字列を配列と乱数を使って天気を表示するプログラム

Posted at

文字列を配列と乱数を使って天気を表示するプログラムを実装しました。

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>文字列の置換テスト</title>
  <style>
    .test1 {
      color:#0000c1;
      font-size:20px;
    }
  </style>
</head>
<body>
  <script>
    // 天気配列
    const array1 = ["snow","cloudy","rain","fine"];
    // 乱数表示0から2
    const num = Math.floor(Math.random()*4);
    const str = "It is fine!";
    let ans = "";
    ans = str.replace('fine',array1[num]);
    
    
    document.write(`<p class="test1"> ${ans} </p>`);

  </script>
</body>
</html>

javaScriptのソースコードです

 // 天気配列
    const array1 = ["snow","cloudy","rain","fine"];
    // 乱数表示0から2
    const num = Math.floor(Math.random()*4);
    const str = "It is fine!";
    let ans = "";
    // 文字の置換
    ans = str.replace('fine',array1[num]);
    //ブラウザに表示
    document.write(`<p class="test1"> ${ans} </p>`);
2
2
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
2
2