본문 바로가기

Programming/JavaScript

The while Loop

let rep = 1;
while (rep <= 10) {
    console.log(`Lifting weight repetition ${rep} `);
    rep++;
}

As long as the condition is true, it keeps running.

let dice = Math.trunc(Math.random() * 6) + 1;

while (dice !== 6) {
    console.log(`You rolled a ${dice}`);
    dice = Math.trunc(Math.random() * 6) + 1;
    if (dice === 6) console.log(`Loop is about to end..`);
}

만약에 처음부터 랜덤값이 6이 나와버리면

그냥 검은 화면 되버림

Why? 시작조차 하지를 못해서!

 

In JavaScript, trunc() is a function that is used to return the integer portion of a number. It truncates the number and removes all fractional digits. Because the trunc() function is a static function of the Math object, it must be invoked through the placeholder object called Math.

 

https://www.techonthenet.com/js/math_trunc.php

 

JavaScript: Math trunc() function

JavaScript: Math trunc() function This JavaScript tutorial explains how to use the math function called trunc() with syntax and examples. Description In JavaScript, trunc() is a function that is used to return the integer portion of a number. It truncates

www.techonthenet.com

 

Math.random()

The Math.random() function returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1, with approximately uniform distribution over that range — which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

 

Math.random() - JavaScript | MDN

The Math.random() function returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1, with approximately uniform distribution over that range — which you can then scale to your desired range. The implementation sele

developer.mozilla.org