범위 (0-100)에있는 세 개의 숫자를 랜덤 화하고 가장 큰 숫자를 인쇄하는 함수를 만들려고합니다.

sammyboy841

...하지만 콘솔에서 함수를 호출하면 undefined를 반환합니다. 나는 JavaScript 초보자이므로 아마도 기본적인 실수를하고있을 것입니다. 누군가 나를 도울 수 있다면 좋을 것입니다. :-).

다음은 코드입니다.

var randomPrint = function(){

x = Math.floor(Math.random() * 100);
y = Math.floor(Math.random() * 100);
z = Math.floor(Math.random() * 100);

   console.log(x, y, z);

   if(x > y && x > z)
   {
     console.log("The greatest number is" + " " + x);
   }
   else if(y > z && y > x)
   { 
     console.log("The greatest number is" + " " + y);
   }
   else if(z > y && z > x)
   {   
    console.log("The greatest number is" + " " + z);
   }
};
randomPrint();
조나스 그루만

속임수의 대답이 더 나은 해결책이지만, 당신의 일도 봅니다. 콘솔의 출력 예는 다음과 같습니다.

35 50 47
The greatest number is 50
undefined

정의되지 않은 부분은 함수가 아무것도 반환하지 않기 때문입니다. 다음과 같이 쓸 수 있습니다.

var randomPrint = function(){

    x = Math.floor(Math.random() * 100);
    y = Math.floor(Math.random() * 100);
    z = Math.floor(Math.random() * 100);

   console.log(x, y, z);

   if(x > y && x > z) {
        var biggest = x;
        console.log("The greatest number is" + " " + x);
   } else if(y > z && y > x) { 
       console.log("The greatest number is" + " " + y);
       var biggest = y;
   } else if(z > y && z > x) {   
       console.log("The greatest number is" + " " + z);
       var biggest = z;
   }
   return biggest;
};

randomPrint();

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관