var rand;
var count = 0;
function gimmeRandom();
{
rand = Math.floor(Math.random()*10)+1;
count++;
return(gimmeRandom);
}
function countToRandom();
{
var count;
for(count = 1; count <= rand; count++)
{
console.log(count);
}
}
console.log("counting to a random number");
gimmeRandom();
countToRandom();
console.log("counting to another random number");
gimmeRandom();
countToRandom();
console.log(there has been "+count+" random numbers");
所以我需要的帮助是使用这段代码,但让countToRandom接受一个参数,然后计算参数中提供的值。
谢谢,
答案 0 :(得分:1)
function countToRandom(count) {
for(var i = 1; i <= count; i++)
{
console.log(i);
}
}
countToRandom(15);
答案 1 :(得分:1)
代码正在运行。只需删除分号和第一个函数中的return-statement
。
<强>缩短强>
var rand;
var count = 0;
function gimmeRandom() {
rand = Math.floor(Math.random() * 10) + 1;
console.log("rand", rand);
count++;
}
function countToRandom() {
for (var i = 1; i <= rand; i++) {
console.log(i);
}
}
console.log("counting to a random number");
gimmeRandom();
countToRandom();
console.log("counting to another random number");
gimmeRandom();
countToRandom();
console.log("there has been " + count + " random numbers");
&#13;
答案 2 :(得分:0)
var rand;
var count = 0;
function gimmeRandom()
{
var rand = Math.floor(Math.random()*10)+1;
return(rand);
}
function countToRandom(rand,count)
{
for(var i = 1; i <= rand; i++)
{
console.log(i);
}
count++;
return(count);
}
console.log("counting to a random number");
rand = gimmeRandom();
count = countToRandom(rand,count);
console.log("counting to another random number");
rand = gimmeRandom();
count = countToRandom(rand,count);
console.log("there has been "+count+" random numbers");