“Running Loop showing results” 可以翻译为 “运行循环显示结果”。

huangapple go评论92阅读模式
英文:

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 = &#39;&#39;;
    for (i = 0; i &lt; 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(&#39;img&#39;);
    
        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 &lt; 20; i++) {
            if(i%2){
                results[i] = &#39;heads&#39;;
            } else{
                results[i] = &#39;tails&#39;;
            }
            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 &lt; 20; cnt++) {
    let result = flipCoin();
    results.push(result);
    console.log(result == 0 ? &quot;Tail&quot; : &quot;Head&quot;);
  }
  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 &lt; 20; i++) {
        if(i%2){
            results[i] = &#39;heads&#39;;
        } else{
            results[i] = &#39;tails&#39;;
        }
        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 =&gt; Math.random()*10 &amp; 1 ? &#39;Head&#39; : &#39;Tails&#39;)		
}

console.log(display20Flips())

<!-- end snippet -->

答案4

得分: 0

I'm not an expert but here it is “Running Loop showing results” 可以翻译为 “运行循环显示结果”。

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;


&lt;script&gt;
var results = [];
function display20Flips() {
  for (var i = 0; i &lt; 20; i++) {
  var x = Math.floor((Math.random() * 2));
  if (x == 0) {
    results[i] = &quot;heads&quot;;
  } else {
  	results[i] = &quot;tails&quot;;
  }
}
console.log(results);
}
display20Flips();
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;

<!-- 结束代码片段 -->

英文:

Im not an expert but here it is “Running Loop showing results” 可以翻译为 “运行循环显示结果”。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;


&lt;script&gt;
var results = [];
function display20Flips() {
  for (var i = 0; i &lt;20; i++ ){
  var x = Math.floor((Math.random() * 2));
  if(x == 0){
    results[i] = &quot;heads&quot;;
  }else{
  	results[i] = &quot;tails&quot;;
  }
}
console.log(results);
}
display20Flips();
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月6日 15:18:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608103.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定