在JavaScript持久阵列 [英] Persistent array in javascript
问题描述
var links = ["http://www.google.com/", "http://www.cnn.com/", "http://www.bbc.com/", "http://www.nbc.com/"];
var random = Math.round(Math.random() * 4);
var previous = [];
previous.push(random);
for (var i = 0; i < previous.length; i++) {
while (previous[i] == random) {
random = Math.round(Math.random() * 4);
}
}
window.location = links[random];
所以,我已经得到了这个位置code。其目的是,一旦用在谷歌网站上的一个按钮启动,给用户带来了一系列的网站之一,随机。我需要它是要记住它需要哪个站点他们(通过记住的Math.random输出)。眼下,各code运行时(模拟用户点击该按钮多次),它会删除我的存储阵列,'previous。我想这code打开一个单独的窗口,它输出到现场。无论是饼干,内部框架或其他方法时,我会很感激,如果有人可以帮助我。
So I've got this here code. It's purpose is, once launched with a button on a google site, to lead the user to one of the set sites, randomly. What I need it to is to remember which site it takes them to (by remembering the Math.random output). Right now, each time the code is run (simulating a user clicking the button many times), it erases my memory array, 'previous'. I want this code to open a separate window for the site it outputs to. Whether cookies, iframes or some other method is used, I'd be very thankful if someone could help me out.
我目前正在经历上的JavaScript codecademy当然,所以请理解,如果我失去了一些东西很简单:)
I'm currently going through the Codecademy course on Javascript, so please understand if I am missing something simple :)
推荐答案
您有几个与你解决的问题:
You have a couple of problems with your solution:
- 当您导航到不同的页面(与
window.location的= ...
),你输了,你有什么变数。这是不可能的,让您的previous数组,如果您导航到一个新的页面,即使你把它连到窗口。 - 您
为
循环使用,而
不是做你的想法。它应该确保你得到你还没有使用一个号码,但有一个缺陷:内,而
可以选择一个随机数,它是previous数组你走过去后,在为
。也就是说,如果previous = [0,2]
,当为
循环检查previous [1] ==随机
,它可以选择0
作为新的随机数,即使它的值previous [0]
。 - 您会INFY,糊涂的,如果你访问过的所有环节。
- When you navigate to a different page (with
window.location = ...
), you lose any variables you have. It's impossible to keep your previous array if you navigate to a new page, even if you attach it to the window. - Your
for
loop with awhile
isn't doing what you think. It should make sure you get a number you haven't used yet, but there's a bug: the innerwhile
can choose a random number that is in the previous array after you've gone past in in thefor
. i.e. ifprevious = [0,2]
, when thefor
loop is checking thatprevious[1] == random
, it could choose0
as the new random number, even though it's the value ofprevious[0]
. - You'll infy-loopy if you've visited all the links.
要解决这个问题,首先你要开始在新窗口中打开页面。参阅这个SO 回答有关如何操作的更多信息。
To fix this, first you have to start opening the pages in a new window. Refer to this SO answer for more information on how to do this.
其次,你需要做的确保你的previous数组不包含值的工作做得更好。一个简单的实施包含
的功能是:
Second, you need to do a better job of making sure that your previous array doesn't contain the value. A simple implementation of a contains
function is:
function contains(array, value) {
for (var i = 0; i < array.length; i++) {
if (array[i] == value) return true;
}
return false;
}
这里的工作JS小提琴你要找什么: http://jsfiddle.net / pnP4D / 7 /
Here's a working JS Fiddle of what you're looking for: http://jsfiddle.net/pnP4D/7/
我保持一个访问数组来存储您访问过的链接;你可以很容易地保持这个作为你的previous阵列和存储的随机数。
I keep a visited array to store the links you've visited; you could just as easily keep this as your previous array and store random numbers.
var links = ["http://www.google.com/", "http://www.cnn.com/", "http://www.bbc.com/", "http://www.nbc.com/"];
var visited = [];
// Assumes you have an element like <button id='btn'>Click me</button>
var button = document.getElementById('btn');
button.addEventListener('click', function() {
// If we've visited all the links, don't try redirecting again
if (visited.length == links.length) {
alert('You visited all the links');
return;
}
// Variables to hold our random number and link
var random, url;
// Keep getting a new random url while it's one we've already visited
do {
random = Math.round(Math.random() * 3);
url = links[random];
} while (contains(visited, url));
// We have a url we haven't visited yet; add it to the visited array
visited.push(url);
// Open the link in a new window so we can hold on to the visited array in this window
var win = window.open(url, '_blank');
win.focus();
});
// A simple function to check if an array contains a value
function contains(array, value) {
for (var i = 0; i < array.length; i++) {
if (array[i] == value) return true;
}
return false;
}
这篇关于在JavaScript持久阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!