英文:
Running Loop showing results
问题
function display20Flips() {
const results = [];
// 使用循环来抛硬币20次,每次将抛掷结果以字符串形式显示在页面上。循环完成后,返回包含每次抛掷结果的数组。
for (let i = 0; i < 20; i++) {
if (i % 2) {
results[i] = '正面';
} else {
results[i] = '反面';
}
}
console.log(results);
}
英文:
How can I use a loop to flip a coin 20 times, each time displaying the result of the flip as a string on the page? After the loop completes, I want to return the array with the result of each flip.
What I have so far:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function display20Flips() {
const results = [];
var i;
var results = '';
for (i = 0; i < 20; i++) {
}
<!-- end snippet -->
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const coin = {
state: 0,
flip: function() {
this.state = Math.floor(Math.random() * 2);
},
toString: function() {
if (this.state === 0) {
return Heads;
} else {
return Tails
}
},
toHTML: function() {
const image = document.createElement('img');
image.src = `$this.toString()}.png`
image.alt = this.toString()
return image;
}
};
function display20Flips() {
const results = [];
// 4. One point: Use a loop to flip the coin 20 times, each time displaying the result of the flip as a string on the page. After your loop completes, return an array with the result of each flip.
for (let i = 0; i < 20; i++) {
if(i%2){
results[i] = 'heads';
} else{
results[i] = 'tails';
}
console.log(results[i])
}
}
display20Flips();
function display20Images() {
const results = [];
// 5. One point: Use a loop to flip the coin 20 times, and display the results of each flip as an image on the page. After your loop completes, return an array with result of each flip.
}
<!-- end snippet -->
答案1
得分: 1
以下是您要翻译的内容:
"您可能想要创建一个FlipCoin函数,该函数确定值,然后在循环中调用此函数,将函数结果值存储在变量中,您可以将其打印到屏幕并添加到结果数组中。
类似这样的代码:
// 返回1或0,即1 = 正面,0 = 反面
function flipCoin() {
var flipValue = Math.floor(Math.random() * 100);
return flipValue % 2;
}
function main() {
let results = [];
for (cnt = 0; cnt < 20; cnt++) {
let result = flipCoin();
results.push(result);
console.log(result == 0 ? "反面" : "正面");
}
console.dir(results);
}
main();
"
英文:
You might want to create a FlipCoin function which determinates the value, call this function in a loop, storing the function result value in a variable, which you can print to screen and add to a results array.
something like this
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// Returns 1 or 0, i.e. 1 = Head, 0 = Tail
function flipCoin() {
var flipValue = Math.floor(Math.random() * 100);
return flipValue % 2;
}
function main() {
let results = [];
for (cnt = 0; cnt < 20; cnt++) {
let result = flipCoin();
results.push(result);
console.log(result == 0 ? "Tail" : "Head");
}
console.dir(results);
}
main();
<!-- end snippet -->
答案2
得分: 0
我相信你想要的是这样的:
function display20Flips() {
var results = [];
for (let i = 0; i < 20; i++) {
if(i%2){
results[i] = 'heads';
} else{
results[i] = 'tails';
}
console.log(results[i])
}
}
display20Flips(); // 用于在代码片段中显示
英文:
I believe you want something like this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function display20Flips() {
var results = [];
for (let i = 0; i < 20; i++) {
if(i%2){
results[i] = 'heads';
} else{
results[i] = 'tails';
}
console.log(results[i])
}
}
display20Flips(); // for displaying in snippet code
<!-- end snippet -->
答案3
得分: 0
Use Array(20)
创建一个大小为20的数组。
Array.map
用于遍历每个元素,使用 Array.fill
来填充数组的 null 值,否则它将无法进行迭代。
Math.random()*10
用于生成一个介于0到10之间的随机数。
如果值是偶数,则返回 "Head",否则返回 "Tails"。
英文:
Use Array(20)
to create an array of size 20
Array.map
to iterate through each element, use Array.fill
to fill the array with null values else it will not iterate through.
Math.random()*10
to generate a random number between 0-10.
If the value is even then returned Head else Tails
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function display20Flips() {
return Array(20).fill().map(e => Math.random()*10 & 1 ? 'Head' : 'Tails')
}
console.log(display20Flips())
<!-- end snippet -->
答案4
得分: 0
I'm not an expert but here it is
<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言: lang-html -->
<!DOCTYPE html>
<html>
<body>
<script>
var results = [];
function display20Flips() {
for (var i = 0; i < 20; i++) {
var x = Math.floor((Math.random() * 2));
if (x == 0) {
results[i] = "heads";
} else {
results[i] = "tails";
}
}
console.log(results);
}
display20Flips();
</script>
</body>
</html>
<!-- 结束代码片段 -->
英文:
Im not an expert but here it is
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<!DOCTYPE html>
<html>
<body>
<script>
var results = [];
function display20Flips() {
for (var i = 0; i <20; i++ ){
var x = Math.floor((Math.random() * 2));
if(x == 0){
results[i] = "heads";
}else{
results[i] = "tails";
}
}
console.log(results);
}
display20Flips();
</script>
</body>
</html>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论