JavaScript #17
1898 ワード
210804
JavaScript #17
ランダムに10の名言を言いましょう.
-quotes.js
10個のquote、作者. randomness
ランダム -Math module
JS付属
Math.random()
0~1のランダムな数値を返します.
Math.random(*)*10の場合、0から10の数字が得られます.
現在の数字の形態から見るとIntegerではなくfloat型です.
Interger兄として受け取るにはどうすればいいですか?
3つの機能があります. round()
四捨五入 ceil()
アップロード floor()
捨てる
現在必要な値は、1~10のランダムな整数です.
Math.床(Math.random(*)*10)として使用できます.
-quotes.js
現在のコードは10個なので10と書いていますが、増やしたり変更したりするためにlengthを使います.
リファレンス
https://nomadcoders.co/javascript-for-beginners/lobby
名言の出典
KakaoTalk表情「名言かと思った」
JavaScript #17
ランダムに10の名言を言いましょう.
-quotes.js
10個のquote、作者.
ランダム
JS付属
Math.random()
0~1のランダムな数値を返します.

Math.random(*)*10の場合、0から10の数字が得られます.
現在の数字の形態から見るとIntegerではなくfloat型です.

Interger兄として受け取るにはどうすればいいですか?
3つの機能があります.
四捨五入
アップロード
捨てる

Math.床(Math.random(*)*10)として使用できます.
-quotes.js
const quotes = [{quote: " ...", authoer: "..."},{....},]
const quote = document.querySelector("#quote span:first-child")
const author = document.querySelector("#quote span:last-child")
console.log(quotes[Math.floor(Math.random() * 10)])

現在のコードは10個なので10と書いていますが、増やしたり変更したりするためにlengthを使います.
const todaysQuote = quotes[Math.floor(Math.random() * quotes.length)]
この名言をスクリーンに出力してみましょう.const quote = document.querySelector("#quote span:first-child")
const author = document.querySelector("#quote span:last-child")
const todaysQuote = quotes[Math.floor(Math.random() * quotes.length)]
quote.innerText = todaysQuote.quote
author.innerText = todaysQuote.author

リファレンス
https://nomadcoders.co/javascript-for-beginners/lobby
名言の出典
KakaoTalk表情「名言かと思った」
Reference
この問題について(JavaScript #17), 我々は、より多くの情報をここで見つけました https://velog.io/@kimhaech/JavaScript-17-k4bv106uテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol