0
1

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 5 years have passed since last update.

0~2のランダムな数値を3つ取得し、「それぞれの数値」と「最も数値が大きい変数」の情報を表示。※「最も数値が大きい変数」が複数ある場合は複数表示

Posted at
<!DOCTYPE html>
<html lang="ja">
<head>
   <meta charset="UTF-8">
   <title>if&amp;3つの数値から最大値を求める</title>
</head>
<body>
   <script>
        var rand1 = Math.floor(Math.random() * 3);
        var rand2 = Math.floor(Math.random() * 3);
        var rand3 = Math.floor(Math.random() * 3);
        document.write('<p>rand1: ' + rand1 + '</p>');
        document.write('<p>rand2: ' + rand2 + '</p>');
        document.write('<p>rand3: ' + rand3 + '</p>');
        
        
        //3つがすべて同じ値の場合
       
        if ( rand1 === rand2 && rand2 === rand3 ) {
            document.write('<p>3つは同じ値です</p>');
        }
        
        if ( rand1 === rand2 ){
            if ( rand2 >rand3) {
                document.write('<p>一番値が大きいのはrand1とrand2です</p>');
            }else {
                document.write('<p>一番値が大きいのはrand3です</p>');
            }
        } else if (rand1 === rand3) {
            if ( rand3 >rand2) {
                document.write('<p>一番値が大きいのはrand1とrand3です</p>');
            }else {
                document.write('<p>一番値が大きいのはrand2です</p>');
            }
        } else if (rand2 === rand3) {
            if ( rand3 >rand1) {
                document.write('<p>一番値が大きいのはrand2とrand3です</p>');
            }else {
                document.write('<p>一番値が大きいのはrand1です</p>');
            }
        } else if ( rand1 > rand2 && rand1 > rand3 ) {
            document.write('<p>一番値が大きいのはrand1です</p>');
        } else if ( rand2 > rand3 && rand2 > rand1 ) {
            document.write('<p>一番値が大きいのはrand2です</p>');
        } else if ( rand3 > rand2 && rand3 > rand1 ) {
            document.write('<p>一番値が大きいのはrand3です</p>');
        }
    </script>
</body>
</html>
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?