- Objective: 测试知识点:如何在函数中添加条件语句。变量,数据类型,条件,函数 - Breadcrumb: JS I # 概念阐释 # 实例 # 相关内容 # 问题 🌟 写一个函数rollTheDice,有2个骰子分别随机出现**1-6**(注意是1-6,不是0-6),得出2个骰子相加的结果。 # 问题答案 *随机出现,所以不需要设置好参数* ```js const rollTheDice = () => { // Math.random() gives us a random number from 0 up to, but not including, 1 // We multiplied that by 6 to get a number between 0 and up to, but not including, 6 // But since we actually wanted numbers from 1 to 6, inclusive, we added 1 let dice1 = 1 + Math.floor(Math.random() * 6 ); let dice2 = 1 + Math.floor(Math.random() * 6 ); return dice1 + dice2; }; console.log(rollTheDice()); ``` # 参考资料 - [fix the broken code](https://www.codecademy.com/journeys/full-stack-engineer/paths/fscj-22-building-interactive-websites/tracks/fscj-22-javascript-syntax-part-i/modules/fscj-22-practice-javascript-syntax-variables-data-types-conditionals-functions/lessons/javascript-fundamentals-code-challenge/exercises/fix-the-broken-code)