Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-选择与当前数组相同的随机数组,然后选择另一个_Javascript_Random_Repeat - Fatal编程技术网

Javascript-选择与当前数组相同的随机数组,然后选择另一个

Javascript-选择与当前数组相同的随机数组,然后选择另一个,javascript,random,repeat,Javascript,Random,Repeat,我的目标是: 我正试图在我的网站上创建一个小的推荐区 我有1份推荐信,当点击按钮时,当前推荐信消失,一份新的随机推荐信出现在框中。这个很好用。 但是 我注意到随机选择器正在抛出重复的证明(显示证明1,单击按钮,证明1仍然偶然出现) 我试图写一个命令,上面写着: 如果新数组与前一个相同,则重复随机选择过程(重做数学) 否则,请编写(innerHTML)新的推荐信 我的问题是我不知道IF部分的编码(我在这里潦草地写了“与当前消息相同”) 下一阶段还将是“转到脚本开始”部分(重做数学) 如果有人能在这

我的目标是:

我正试图在我的网站上创建一个小的推荐区

我有1份推荐信,当点击按钮时,当前推荐信消失,一份新的随机推荐信出现在框中。这个很好用。 但是 我注意到随机选择器正在抛出重复的证明(显示证明1,单击按钮,证明1仍然偶然出现)

我试图写一个命令,上面写着: 如果新数组与前一个相同,则重复随机选择过程(重做数学) 否则,请编写(innerHTML)新的推荐信

我的问题是我不知道IF部分的编码(我在这里潦草地写了“与当前消息相同”)

下一阶段还将是“转到脚本开始”部分(重做数学)

如果有人能在这里帮助我,我会非常感激,因为我有点不知所措

先谢谢你

function quotes() {
    //Define and populate the array 
    var aquote = new Array;
    aquote[0] = "\"Your cakes are fantastic, beautiful designs and taste gorgeous!\"";
    aquote[1] = "\"I can’t believe how beautiful the cake was and how much detail there was on it.  My mum cried when she saw it and didn’t want to cut it up but we eventually persuaded her and it was really tasty.\" Sasha – Rothwell";
    aquote[2] = "\"Thank you for our wedding cake.  The fruit cake was absolutely delicious and so moist.  The flowers you made were beautiful and exactly as we imagined they would be.  We have kept the flowers and they are a great reminder of our wonderful day.\" Paul & Jane – Rutland"
    aquote[3] = "\"My husband said that the cupcakes you made for his birthday are the best he has tasted and your buttercream is divine – I have to agree!\" Dawn – Cambridgeshire"
    aquote[4] = "\"Thank you Bumble Cottage Cakes for My son’s birthday cake it was fantastic as usual I will be back soon and I can’t wait for the next one.\"Liz  – Desborough"

    //Generate a random number then print the quote from that array index
    rdmQuote = Math.floor(Math.random() * aquote.length);
    if (rdmQuote = aquote[SAME AS CURRENT MESSAGE]) {
        alert('quote is same as current')
    } else {
        document.getElementById("randomtestimonial").innerHTML = aquote[rdmQuote];
    }
}

我只缓存当前选择的内容

  <!-- Hide the script from old browsers //-->
 var _cache="";
 function quotes(){
 //Define and populate the array 
 var aquote = new Array;
   // all your quotes

 //Generate a random number then print the quote from that array index
 rdmQuote = Math.floor(Math.random()*aquote.length);

 if (_cache == rdmQuote) 
     {
         alert('quote is same as current')
     }
     else
     {
       document.getElementById("randomtestimonial") .innerHTML=aquote[rdmQuote];
 }
 _cache  = rdmQuote;
 }

var_cache=“”;
函数引号(){
//定义并填充数组
var aqueote=新数组;
//你所有的报价
//生成一个随机数,然后从该数组索引打印引号
rdmQuote=Math.floor(Math.random()*aquote.length);
if(_cache==rdmQuote)
{
警报('报价与当前报价相同')
}
其他的
{
document.getElementById(“随机推荐”).innerHTML=aquote[rdmQuote];
}
_cache=rdmQuote;
}

我只需缓存当前选择

  <!-- Hide the script from old browsers //-->
 var _cache="";
 function quotes(){
 //Define and populate the array 
 var aquote = new Array;
   // all your quotes

 //Generate a random number then print the quote from that array index
 rdmQuote = Math.floor(Math.random()*aquote.length);

 if (_cache == rdmQuote) 
     {
         alert('quote is same as current')
     }
     else
     {
       document.getElementById("randomtestimonial") .innerHTML=aquote[rdmQuote];
 }
 _cache  = rdmQuote;
 }

var_cache=“”;
函数引号(){
//定义并填充数组
var aqueote=新数组;
//你所有的报价
//生成一个随机数,然后从该数组索引打印引号
rdmQuote=Math.floor(Math.random()*aquote.length);
if(_cache==rdmQuote)
{
警报('报价与当前报价相同')
}
其他的
{
document.getElementById(“随机推荐”).innerHTML=aquote[rdmQuote];
}
_cache=rdmQuote;
}

只需使用变量存储当前报价索引即可

var current = -1;

function quotes() {

    //Define and populate the array 
    var aquote = new Array;
    aquote[0] = "\"Your cakes are fantastic, beautiful designs and taste gorgeous!\"";
    aquote[1] = "\"I can’t believe how beautiful the cake was and how much detail there was on it.  My mum cried when she saw it and didn’t want to cut it up but we eventually persuaded her and it was really tasty.\" Sasha – Rothwell";
    aquote[2] = "\"Thank you for our wedding cake.  The fruit cake was absolutely delicious and so moist.  The flowers you made were beautiful and exactly as we imagined they would be.  We have kept the flowers and they are a great reminder of our wonderful day.\" Paul & Jane – Rutland"
    aquote[3] = "\"My husband said that the cupcakes you made for his birthday are the best he has tasted and your buttercream is divine – I have to agree!\" Dawn – Cambridgeshire"
    aquote[4] = "\"Thank you Bumble Cottage Cakes for My son’s birthday cake it was fantastic as usual I will be back soon and I can’t wait for the next one.\"Liz  – Desborough"

    //Generate a random number then print the quote from that array index
    rdmQuote = Math.floor(Math.random() * aquote.length);
    if (rdmQuote == current) {
        alert('quote is same as current')
    } else {
        document.getElementById("randomtestimonial").innerHTML = aquote[rdmQuote];
        current = rdmQuote;
    }
}

只需使用一个变量来存储当前报价索引

var current = -1;

function quotes() {

    //Define and populate the array 
    var aquote = new Array;
    aquote[0] = "\"Your cakes are fantastic, beautiful designs and taste gorgeous!\"";
    aquote[1] = "\"I can’t believe how beautiful the cake was and how much detail there was on it.  My mum cried when she saw it and didn’t want to cut it up but we eventually persuaded her and it was really tasty.\" Sasha – Rothwell";
    aquote[2] = "\"Thank you for our wedding cake.  The fruit cake was absolutely delicious and so moist.  The flowers you made were beautiful and exactly as we imagined they would be.  We have kept the flowers and they are a great reminder of our wonderful day.\" Paul & Jane – Rutland"
    aquote[3] = "\"My husband said that the cupcakes you made for his birthday are the best he has tasted and your buttercream is divine – I have to agree!\" Dawn – Cambridgeshire"
    aquote[4] = "\"Thank you Bumble Cottage Cakes for My son’s birthday cake it was fantastic as usual I will be back soon and I can’t wait for the next one.\"Liz  – Desborough"

    //Generate a random number then print the quote from that array index
    rdmQuote = Math.floor(Math.random() * aquote.length);
    if (rdmQuote == current) {
        alert('quote is same as current')
    } else {
        document.getElementById("randomtestimonial").innerHTML = aquote[rdmQuote];
        current = rdmQuote;
    }
}

一种方法是将元素的内容与随机选择的新内容进行比较,并使用循环选择新内容,直到它们不同

var currentContent = document.getElementById("randomtestimonial").innerHTML;
do {
  rdmQuote = aquote[Math.floor(Math.random()*aquote.length)];
} while(currentContent == rdmQuote);
document.getElementById("randomtestimonial").innerHTML = rdmQuote;

这段代码可以改进,因为如果
aquote.length
曾经是1,那么这将是一个无限循环。但是,希望这是一个很好的起点。

一种方法是将元素的内容与随机选择的新内容进行比较,并使用循环选择一个新内容,直到它们不同为止

var currentContent = document.getElementById("randomtestimonial").innerHTML;
do {
  rdmQuote = aquote[Math.floor(Math.random()*aquote.length)];
} while(currentContent == rdmQuote);
document.getElementById("randomtestimonial").innerHTML = rdmQuote;

这段代码可以改进,因为如果
aquote.length
曾经是1,那么这将是一个无限循环。但是,希望这是一个很好的起点。

我将定义数组,使其只初始化一次。然后,我将选择一个介于0和n-2之间的随机条目m(数组大小为n),并将第m个条目与n-1条目交换,然后显示该条目。因此,新的选择永远无法选择当前显示的条目。

我将定义数组,使其只初始化一次。然后,我将选择一个介于0和n-2之间的随机条目m(数组大小为n),并将第m个条目与n-1条目交换,然后显示该条目。因此,新的选择永远无法选择当前显示的条目。

存储最后一个报价的索引并进行比较

var lastQuote = -1;
function quotes() {
    //Define and populate the array 
    var aquote = new Array;
    aquote[0] = "\"Your cakes are fantastic, beautiful designs and taste gorgeous!\"";
    aquote[1] = "\"I can’t believe how beautiful the cake was and how much detail there was on it.  My mum cried when she saw it and didn’t want to cut it up but we eventually persuaded her and it was really tasty.\" Sasha – Rothwell";
    aquote[2] = "\"Thank you for our wedding cake.  The fruit cake was absolutely delicious and so moist.  The flowers you made were beautiful and exactly as we imagined they would be.  We have kept the flowers and they are a great reminder of our wonderful day.\" Paul & Jane – Rutland"
    aquote[3] = "\"My husband said that the cupcakes you made for his birthday are the best he has tasted and your buttercream is divine – I have to agree!\" Dawn – Cambridgeshire"
    aquote[4] = "\"Thank you Bumble Cottage Cakes for My son’s birthday cake it was fantastic as usual I will be back soon and I can’t wait for the next one.\"Liz  – Desborough"
    //Generate a random number then print the quote from that array index
    var rdmQuote = Math.floor(Math.random() * aquote.length);   
    if (rdmQuote == lastQuote) {
        alert('quote is same as current')
    } else {
        document.getElementById("randomtestimonial").innerHTML = aquote[rdmQuote];
        lastQuote = rdmQuote;
    }
}

存储最后一个报价的索引并进行比较

var lastQuote = -1;
function quotes() {
    //Define and populate the array 
    var aquote = new Array;
    aquote[0] = "\"Your cakes are fantastic, beautiful designs and taste gorgeous!\"";
    aquote[1] = "\"I can’t believe how beautiful the cake was and how much detail there was on it.  My mum cried when she saw it and didn’t want to cut it up but we eventually persuaded her and it was really tasty.\" Sasha – Rothwell";
    aquote[2] = "\"Thank you for our wedding cake.  The fruit cake was absolutely delicious and so moist.  The flowers you made were beautiful and exactly as we imagined they would be.  We have kept the flowers and they are a great reminder of our wonderful day.\" Paul & Jane – Rutland"
    aquote[3] = "\"My husband said that the cupcakes you made for his birthday are the best he has tasted and your buttercream is divine – I have to agree!\" Dawn – Cambridgeshire"
    aquote[4] = "\"Thank you Bumble Cottage Cakes for My son’s birthday cake it was fantastic as usual I will be back soon and I can’t wait for the next one.\"Liz  – Desborough"
    //Generate a random number then print the quote from that array index
    var rdmQuote = Math.floor(Math.random() * aquote.length);   
    if (rdmQuote == lastQuote) {
        alert('quote is same as current')
    } else {
        document.getElementById("randomtestimonial").innerHTML = aquote[rdmQuote];
        lastQuote = rdmQuote;
    }
}

你的工作比你的题目所暗示的要简单。您希望从数组中随机选取一个字符串,如果它与当前的字符串相同,请选取另一个

您也已经知道如何访问此字符串,正在执行以下操作

rdmQuote = Math.floor(Math.random() * aquote.length);
if (rdmQuote == document.getElementById("randomtestimonial").innerHTML]) {
    alert('quote is same as current')
} else {
    document.getElementById("randomtestimonial").innerHTML = aquote[rdmQuote];
}
请注意,我使用了双等于,而不是您使用的单等于是作业和===是相等比较。这仍然给你留下了一个问题,如果它是相同的,你该怎么做(除了提醒)。您需要
do/while
控制命令

var quote = "";
do {
    rdmQuote = Math.floor(Math.random() * aquote.length);
    quote = aquote[rdmQuote];
} while (quote == document.getElementById("randomtestimonial").innerHTML;
document.getElementById("randomtestimonial").innerHTML = quote;

你的工作比你的题目所暗示的要简单。您希望从数组中随机选取一个字符串,如果它与当前的字符串相同,请选取另一个

您也已经知道如何访问此字符串,正在执行以下操作

rdmQuote = Math.floor(Math.random() * aquote.length);
if (rdmQuote == document.getElementById("randomtestimonial").innerHTML]) {
    alert('quote is same as current')
} else {
    document.getElementById("randomtestimonial").innerHTML = aquote[rdmQuote];
}
请注意,我使用了双等于,而不是您使用的单等于是作业和===是相等比较。这仍然给你留下了一个问题,如果它是相同的,你该怎么做(除了提醒)。您需要
do/while
控制命令

var quote = "";
do {
    rdmQuote = Math.floor(Math.random() * aquote.length);
    quote = aquote[rdmQuote];
} while (quote == document.getElementById("randomtestimonial").innerHTML;
document.getElementById("randomtestimonial").innerHTML = quote;

当调用此函数时,
current
不会重置为-1吗?查看我的是否满足您的需要。当调用此函数时,
current
不会重置为-1吗?查看我的是否满足您的需要。谢谢。这似乎仍在抛出重复项。@MichaelWalkling这意味着对
document.getElementById(“RandomTestional”).innerHTML的调用返回的字符串与
aNote[Math.floor(Math.random()*aNote.length)]
@MichaelWalkling的一个建议不同(如果您尚未使用它的话)在FireFox中使用FireBug来帮助诊断这些javascript问题。这是一个很棒的工具。在本例中,调用
console.log(“+document.getElementById(“randomEstimational”).innerHTML+“vs”+aquote[Math.floor(Math.random()*aquote.length)])循环内部将快速向您显示发生了什么。谢谢。这似乎仍在抛出重复项。@MichaelWalkling这意味着对
document.getElementById(“RandomTestional”).innerHTML的调用返回的字符串与
aNote[Math.floor(Math.random()*aNote.length)]
@MichaelWalkling的一个建议不同(如果您尚未使用它的话)在FireFox中使用FireBug来帮助诊断这些javascript问题