JavaScript 变量

发布于 2024-11-26 16:06:15 字数 895 浏览 1 评论 0原文

我正在努力获取一个变量更改另一个不相关变量的结果。

我有一个脚本生成 0 到 19 之间的随机数,该随机数将自身附加到全局变量“index”。我想要另一个脚本,它可以读取“索引”变量的结果,并从不同的数组分配适当的文本响应,并将该文本发布到新变量中(让我们称之为“响应”)。这两个变量也需要匹配,文本(“响应”)跟随关联的数字(“索引”)。例如,如果 var index=0,则 var response=“good”,当 var index=1 时,var response=“bad”,依此类推,将所有 20 个可能的结果放入每个数组。

它看起来很简单,但我无法接受非常复杂和低效(即不兼容)的方法。

提前非常感谢您,那里有一些非常有才华的人!

感谢您的及时回复!

这是一些代码。

    var answers= new Array(20);
    for (i = 0; i < answers.length; i++)
        answers[i] = new Image();
        answers[0].src = 'images/answer1.jpg';
        //so on an so forth from 0 - 19
    var index;  

    function askQuestion(){
        index = Math.floor(Math.random() * (answers.length));}  

因此,我有 var 索引返回触发关联图像的值,但随后也想使用索引 var 的结果来输出关联文本(使用另一个 var)。我不敢相信我竟然被这么简单的事情难住了!我认为我用多个变量使其变得过于复杂,或者再次将代码加倍。也许我只是在填充语法。该死,我的 javascript 编码不是最好的。那些年前就不应该放弃数学!有什么想法吗?

I'm struggling to get the result of one variable change another unrelated variable.

I have a script that is generating a random number between 0 and 19, which attaches itself to the global variable "index". I'd like to have another script that can read the result of the "index" variable and assign the appropriate text response from a different array, and post that text into a new variable (lets call it "response"). These two variables need to match up as well, the text ("response") following the associated number ("index"). e.g if the var index=0 then the var response= "good", when var index=1 then var response="bad" so on an so forth for all 20 possible outcomes put each array.

It seems pretty simple, but has eluded me accept for very complex and inefficient (i.e incompatible) means.

Thank you so much in advance, there's some very talented peeps out there!

Thanks for your prompt responses!

Here's some of the code.

    var answers= new Array(20);
    for (i = 0; i < answers.length; i++)
        answers[i] = new Image();
        answers[0].src = 'images/answer1.jpg';
        //so on an so forth from 0 - 19
    var index;  

    function askQuestion(){
        index = Math.floor(Math.random() * (answers.length));}  

So I've got the var index returning values which trigger the associated image, but then want to use the result of the index var to output an associated text too (using the another var). I can't believe I'm stumped on such a simple thing! I think I'm over complicating it with multiple variables or doubling the code again. Perhaps I'm just stuffing up the syntax. Damn, my javascript coding aint the greatest. Shouldn't of dropped out of maths all those years ago! Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱你不解释 2024-12-03 16:06:15

你只需要这个吗?

var response = yourDifferentArray[window.index];

语法 window[varName] 允许您从代码中的任何位置检索全局变量的值。

Do you just need this?

var response = yourDifferentArray[window.index];

The syntax window[varName] allows you to retrieve the value of a global variable from anywhere in your code.

離人涙 2024-12-03 16:06:15

所以这真的很简单。我的问题是尝试使用多个不进行通信的脚本太棘手(并且存在语法错误)。这是结果。

var answers= new Array(20);
for (i = 0; i < answers.length; i++)
    answers[i] = new Image();
    answers[0].src = 'images/answer1.jpg';
    //so on an so forth from 0 - 19
var index;    
var remarks = ["remark0","remark1"] //..so on 0-19
var response;

function askQuestion(){
    window.index = Math.floor(Math.random() * (answers.length));}
    response = remarks[window.index];

非常感谢您的帮助!金星!!

So it was really simple. My problem was being too tricky (and a syntax error) by trying to use multiple scripts which weren't communicating. Here's the result.

var answers= new Array(20);
for (i = 0; i < answers.length; i++)
    answers[i] = new Image();
    answers[0].src = 'images/answer1.jpg';
    //so on an so forth from 0 - 19
var index;    
var remarks = ["remark0","remark1"] //..so on 0-19
var response;

function askQuestion(){
    window.index = Math.floor(Math.random() * (answers.length));}
    response = remarks[window.index];

Thank you so much for the help! GOLD STAR!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文