如何比较两个数组的索引值?

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

How to compare index value of 2 arrays?

问题

我已经学习了3个月的教育,将学习不同的编程语言,如HTML、CSS、JS等等。目前,我正在努力寻找解决方案,主要是因为我是新手,不太确定该寻找什么。我正在创建一个网站(不会上传到互联网,只是展示给老师看),在网站上我会放一些小游戏,以展示我迄今为止学到的东西,以及我能做什么(当然我们可以搜索互联网上的信息)。

所以,我将使用一个例子让您知道我在做什么,尽管这不是我在项目中实际做的,但功能是一样的。

我有两个数组。第一个数组包含10个国家,第二个数组包含10个首都。第二个数组中的第5个元素是第一个数组中第5个国家的首都。

对所有选项都是如此。我还创建了一个DIV标签,其中Math.random随机选择一个国家,并将其显示在DIV容器中。我还有一个输入类型为"text"的字段,供用户输入该国家的首都。

这就是对我来说变得棘手的地方,我怎样才能检查用户输入的字符串是否与另一个数组具有相同的索引值(我认为这就是它被称为的原因?)?我完全不知道如何做到这一点。

我当然可以使用很多if语句,比如if(userInput == Washington)然后弹出警告("正确!")。

但这不是我想要的方式,因为我将来会为用户添加添加自己的国家/首都的选项。

我真的觉得这变得混乱了,如果您不明白,请随时提问,我会尽量更详细地解释,谢谢您的耐心,干杯!

英文:

I'm 3 months in to my education where i will learn different languages as HTML,CSS,JS and a lot more. At this moment I'm currently stuggling with finding a solution, mostly because im new and i'm not really sure what to look for. I'm creating a website (will not upload to internet, only show for teacher) where i have different types of small games. Just to show what i've learnt so far, and what i can do (we are allowed to search the web of course).

So i will use an example just for you to know what im dealing with, this how ever is not the exact same im doing in my project, but the function is.

I have 2 arrays. The first array contain 10 countries, the second array contains 10 capital cities. The capital on place 5 in the capital array, is the capital city of the country on place 5 in my coutry array.

And this goes for all options. I've also made a DIV tag where Math.random picks 1 country at random.... and displays it in the DIV container. And i got a input type="text" field for the user to type in the captial city for that country.

This is where it gets tricky for me, how can i check if what they wrote as a string, has the same index value (I think its called that?) as the other array? I'm totaly lost on how this is done.

I could of course use a lot of if statements, like if(userInput == Washington) then alert("Correct!").

However this is not how i want to do it, because i will later add an option for the user to add their own coutries/captials.

I really feel like this got messy, so please ask questions if you don't understand and i will try to break it down further, thanks for you patience, cheers!

答案1

得分: 1

因为您的数组Country和数组Capital具有相同的索引我假设它们始终会这样)。您可以使用Math.random函数生成一个数字。(就像您所做的那样

您已经知道答案因为Country[1]应该有与Captial[1]相同的答案
所以您想要检查的是他们写的是否等于您的答案而这是您所知道的事情

所以我们想知道用户输入是否等于Math.random索引处的Capital数组

如果userinput === Captial[这里是Math.random的数字]//执行您的操作

有些函数可以帮助您检查字符串因为如果您有伦敦伦敦”,它可能会影响答案的正确性

我认为最重要的事情不是代码而是思考过程

__
根据您提供的代码有一些问题

首先`userEnter == glosorSV[ri]`变量ri没有被定义这有两个原因您想要生成一个随机数的函数从未被调用而且即使您调用它它也不会返回一个数字

     function randomizeWord(){
     var ri = Math.floor(Math.random() * glosorEng.length);
    document.getElementById("contentG").innerHTML = glosorEng[randomGlosa];

您有这个函数它会创建一个随机数但是然后您使用变量`randomGlosa`来选择数组中的一个元素`randomGlosa`并未定义包含随机数的变量称为ri因此您希望将其更改为`glosorEng[ri]`或将`var ri`更改为`var randomGlosa`

下一个问题是这个函数没有返回任何内容它是一个void函数我不知道您在课程中已经学到了多少关于函数的内容
Void函数用于当主程序不需要处理任何输出时在这种情况下我们关心输出因为我们需要一个随机数此外除非您的任务要求它是一个函数否则它根本不需要是一个函数因此我们可以定义一个在整个脚本中可用的变量或者您应该给它一个返回值具体取决于您如何调用该函数

您创建的for循环没有任何目的您遍历一个列表但根本不使用元素此外`i=0`应该是i的声明所以`var i=0``let i = 0`关于var和let除非您的任务和阅读材料不使用它否则您应该查找let和const来声明变量然后要注意另一种较新的声明方法

如果删除for循环返回值是不需要的因为函数会自动停止因为您的返回值没有值它将执行相同的操作

所以它应该是这样的

//在这里定义您的变量:
var 1 = ...
var 2 = ...
var ri = 0;

function randomizeWord(){
ri = Math.floor(Math.random() * glosorEng.length);
document.getElementById("contentG").innerHTML = glosorEng[ri];
}

function checkAnswer(){
//获取用户输入
if(userEnter === glosorSV[ri])
//在这里设置警报的逻辑
}

我假设这些函数是通过带有onclick事件的按钮调用的
英文:

Since your array Country and array Capital have the same indexes (and I assume they will always have). You can use the Math.random function to generate a number. (Like you do)

You already know the answer, since Country[1] should have as answer Captial[1]
So what you want to check if what they wrote equals your answer. And that is the thing you know.

So we want to know if the user input equals the Capital array at math.random index:

  If(userinput === Captial[math.random number here]) //do your stuff

there are functions that can help you with checking the string. Because If you have "London" or "london" it might differ the correctness of the answer.

I think the most important thing, isn't the code. But the thinking processes.

__
Edit: Based on the code you provided there are a few things.

First of all, userEnter == glosorSV[ri] the variable ri has not been defined. This has two reasons: the function where you want to generate a random number is never called, and even if you call it it doesn't return a number.

 function randomizeWord(){
var ri = Math.floor(Math.random() * glosorEng.length);
document.getElementById("contentG").innerHTML = glosorEng[randomGlosa];

You have this function and it will create a random number. But then you take the variable randomGlosa to pick an element in your array, while randomGlosa isn't defined. The variable that contains the random number is called ri, so you want to change this to glosorEng[ri] or you want to change var ri to var randomGlosa

The next problem is that this function doesn't give anything back. It's a void function. I have no clue how far you are with functions in your course.
Void functions are used when there is no output that the 'main' program needs to process. In this case, we do care about since we need a random number. Besides, it doesn't have to be a function at all unless your assignment wants it to be a function. So we can define a variable that's available in the whole script or you should give it a return value. Based on how you call the function.

The for-loop you created has no purpose. You take a list and loop through each element, but you don't use an element at all. Beside the i=0 should be a declaration for i so var i=0 or let i = 0. About var and let, you should look up let and const to declare variables unless your assignment and reading materials don't use it. Then be aware of another, newer, method of declaration exists.

If you remove the for-loop the return is not needed, as the function will automatically stop since your return doesn't have a value. It will do the same.

So how should it look like:

//Have your vars here:
var 1 = ...
var 2 = ...
var ri = 0;
function randomizeWord(){
ri = Math.floor(Math.random() * glosorEng.length);
document.getElementById("contentG").innerHTML = glosorEng[ri];
}
function checkAnswer(){
//Get user input
if(userEnter === glosorSV[ri])
//Logic here for the alerts
} 

I assume the functions are being called by buttons with an onclick event.

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

发表评论

匿名网友

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

确定