简体   繁体   English

在数组内部搜索数字

[英]Searching for number inside of an array

Forgive me, I am not quite skilled and fairly new to the language and programming practices. 原谅我,我对语言和编程实践不是很熟练,也不太陌生。 I am creating a button which prompts the user to enter a number once the number is entered, I create a for loop that iterates through the same amount as the number. 我正在创建一个按钮,当输入数字时,它提示用户输入数字,我创建了一个for循环,该循环遍历与数字相同的数量。 For example, the user enters 4 and the screen will display 0 1 2 3 and then I have a button that asks the user to enter a number to see if that number exists in the previous array. 例如,用户输入4,屏幕将显示0 1 2 3,然后我有一个按钮,要求用户输入一个数字,以查看该数字在先前的数组中是否存在。 So if the user entered 3 it would dispay "it exists" if they entered 5 it would display "number not found". 因此,如果用户输入3,则输入“ 5”将不显示“它存在”。 Should I create an array to store the iterations and then run that array through a function that searches for the number. 我应该创建一个数组来存储迭代,然后通过一个搜索该数字的函数来运行该数组。 Looking for guidance, thank you for the help guys. 寻求指导,谢谢您的帮助。

  function getNumber() { var el = document.getElementById("demo"); // Get the user's input and convert it to a number var n = parseInt(prompt("Please enter a number"),10); // Set up a string that will become the output. var output = " "; // loop through given number for(var i = 0; i < n; i++){ // variable containing iterations output += i; //var numArray[i] = output; } //display iterations el.textContent = output; } function findNumber(){ var sn = parseInt(prompt("Search for number"),10); for(var j = 0; j < sn; j++){ } } 
 <button onclick="getNumber()">Click!</button> <p id="demo"></p> <button onclick ="findNumber()">Click!</button> 

Make your n variable global 使您的n变量成为全局变量
Than compare if sn is higher than n 如果sn大于n

 var n; // Make it global function getNumber() { var el = document.getElementById("demo"); // Get the user's input and convert it to a number n = parseInt(prompt("Please enter a number"), 10); // Set up a string that will become the output. var output = " "; // loop through given number for (var i = 0; i < n; i++) { // variable containing iterations output += i; //var numArray[i] = output; } //display iterations el.textContent = output; } function findNumber() { var sn = parseInt(prompt("Search for number"), 10); var isHigher = sn > n; // n is now accessible in this function var message = isHigher ? "Not found" : "Number found!"; alert( message ); } 
 <button onclick="getNumber()">Click!</button> <p id="demo"></p> <button onclick="findNumber()">Click!</button> 

In this theoretic example, it's true that you only need to check if the second number entered is smaller than the first number. 在此理论示例中,确实只需要检查输入的第二个数字是否小于第一个数字。 If you want to search for a number in an array of any numbers, you can use the javascript indexOf function. 如果要在任何数字的数组中搜索数字,可以使用javascript indexOf函数。 See example below: 请参见下面的示例:

 var arr = [1,6,77,888]; if (arr.indexOf(66) > -1) { alert('number is in array'); } else { alert('number is not in array'); } 

To search an array, use the indexOf() method of a JavaScript array. 要搜索数组,请使用JavaScript数组的indexOf()方法。 The original post gives an example populating the array with myArray[x]=x , creating options pointed out by other solutions. 原始文章提供了一个使用myArray[x]=x填充数组的示例,创建了其他解决方案指出的选项。 Presuming you want to search a more general case of an array, you could use indexOf directly or define a function that returns true or false : 假设您要搜索更一般的数组,可以直接使用indexOf或定义一个返回truefalse的函数:

function inArray(myArray, queryValue) {
    return myArray.indexOf(queryValue) > -1;
};

Arrays in JavaScript are objects with some additional methods like pop() , indexOf() , etc. JavaScript objects are associative arrays; JavaScript中的数组是带有一些其他方法的对象,例如pop()indexOf()等。JavaScript对象是关联数组;它们是数组。 this solution only applies to Array objects. 此解决方案仅适用于Array对象。 Array objects are constructed with the [] literal or the Array() constructor. 数组对象是使用[]文字或Array()构造函数构造的。 Arrays can only have properties named by ints, unlike other JavaScript associative arrays. 与其他JavaScript关联数组不同,数组只能具有以int命名的属性。 See Eloquent JavaScript . 参见Eloquent JavaScript

There are a couple of ways to do this. 有两种方法可以做到这一点。 For the sake of simplicity, I'll use a global variable here. 为了简单起见,我将在此处使用全局变量。

 // A global variable to store user input var userInput; function getNumber() { var el = document.getElementById("demo"); // Get the user's input and convert it to a number var n = parseInt(prompt("Please enter a number"),10); // Store the user's input to our global variable userInput = n; // Set up a string that will become the output. var output = " "; // loop through given number for(var i = 0; i < n; i++){ // variable containing iterations output += i; //var numArray[i] = output; } //display iterations el.textContent = output; } function findNumber(){ var el = document.getElementById("result"); var sn = parseInt(prompt("Search for number"),10); // If number is within the range if (sn < userInput) { el.textContent = "it exists"; } // If number is not within the range else { el.textContent = "number not found"; } } 
 <button onclick="getNumber()">Click!</button> <p id="demo"></p> <button onclick ="findNumber()">Click!</button> <p id="result"></p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM