【发布时间】:2021-11-05 02:11:24
【问题描述】:
此代码应该使用 while 循环和 if 语句在 javascript 中显示给定名称的随机字符/字母。 . . 我面临的问题是 RandomLetterIndex 介于 0 和 5 (
const MyName = "Ayman";
var RandomLetterIndex = Math.floor(Math.random() * 10);
while (RandomLetterIndex > MyName.length) {
RandomLetterIndex = Math.floor(Math.random() * 10);
if (RandomLetterIndex < MyName.length && RandomLetterIndex !== 5) {
break
}
}
console.log(RandomLetterIndex);
console.log(MyName.charAt(RandomLetterIndex));
【问题讨论】:
-
你不需要那个while循环。只需调整您的随机器:
const RandomLetterIndex = Math.floor(Math.random() * (MyName.length - 0) + 0);并简单地记录结果。
标签: javascript if-statement while-loop