javascript在数组中搜索整数

时间:2017-02-24 19:56:46

标签: javascript arrays

这是我在用户生成的数组中搜索整数的代码。我无法弄清楚如何让代码实际搜索数组中的代码而不是简单地说明搜索的整数。任何帮助,将不胜感激!



var count = "";
var arr;

function myFunction() {
  var count = prompt("Input array size:");
  document.getElementById("demo_a").innerHTML = "Total Count= " + count;
  arr = [count];
  for (i = 0; i < count; i++) {
    arr[i] = prompt("Enter an integer:");
  }
  document.getElementById("demo_b").innerHTML = arr.toString();
  bubbleSort(arr);
  document.getElementById("demo_b").innerHTML = arr.toString();
}

function myPrompt() {
  var search = prompt("Please enter an integer to search for:");
  document.getElementById("demo_c").innerHTML = "Search For: " + search;
  binarySearch(arr, search);
}

function bubbleSort(arr) {
  var i, j, temp = 0;
  for (i = 0; i < arr.length - 1; i++) {
    for (j = 0; j < arr.length - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        temp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = temp;
      }
    }
  }
}
&#13;
<button type="button" onclick="myFunction()">Input data</button>
<button type="button" onclick="myPrompt()">Find Integer</button>
<p id="demo_a"></p>
<p id="demo_b"></p>
<p id="demo_c"></p>
<p id="demo_d"></p>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

我假设数组只有整数。

删除全局变量并将数组创建为

  

var arr = new Array(count);

然后,我正在使用数组 indexOf 方法进行搜索。

/* lets make a proper date parameter */
/* @date nvarchar format is  YYYY/MM/DD, replacing / gives us YYYYMMDD 
   which sql server can easily convert to a date    */
declare @ActualDate date;
set @ActualDate = dateadd(day,-30,convert(date,replace(@date,'/','')));

select
    coalesce(S.Date,B.Date) Date,
    coalesce(S.TAccount,0) sTAccount,
    coalesce(S.Remaining,0) sRemaining,
    coalesce(B.TAccount,0) bTAccount,
    coalesce(B.Remaining,0) bRemaining
from
    (select
        Date,sum(TAccount) TAccount, sum(Remaining) Remaining
    from SaleInvoices
    where convert(date,replace(Date,'/','')) => @ActualDate
    group by Date
    ) S
    Full Outer Join
    (select
         Date,sum(TAccount) TAccount, sum(Remaining) Remaining
    from BuyInvoices
    where convert(date,replace(Date,'/','')) => @ActualDate
    group by Date 
    ) B
    on S.Date=B.Date

答案 1 :(得分:0)

如果这仅用于练习或学习,请检查以下答案。 否则,您必须考虑其他技术,例如不使用警报输入等;对于小数据集,javascript可以进行排序和搜索,也可以考虑@CollinD所说的内容。

无论如何,请检查以下代码:

var count = '';
var arr;

function myFunction() {
  var count = prompt("Input array size:");
  document.getElementById("demo_a").innerHTML = "Total Count= " + count;
  arr = [count];
  for (i = 0; i < count; i++) {
    arr[i] = prompt("Enter an integer:");
  }
  document.getElementById("demo_b").innerHTML = arr.toString();
  bubbleSort(arr);
  document.getElementById("demo_b").innerHTML = arr.toString();
}

function myPrompt() {
  var search = prompt("Please enter an integer to search for:");
  document.getElementById("demo_c").innerHTML = "Search For: " + search;
  linearSearch(arr, search);
}

function linearSearch(arr, search) { 

  var found = false; 
  var foundAt; 

  arr.some(function(val, index) {

    if (val == search) {

      foundAt = index;   
      found = true;
      return true;  
    }

  }); 
  if (found) {  
    document.getElementById("demo_d").innerHTML = "Found " + search + ' at index ' + foundAt;  
  } else {   
    document.getElementById("demo_d").innerHTML = "Not Found " + search;   
  }
}

function bubbleSort(arr) {
  var i, j, temp = 0;
  for (i = 0; i < arr.length - 1; i++) {
    for (j = 0; j < arr.length - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        temp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = temp;
      }
    }
  }
}
<button type="button" onclick="myFunction()">Input data</button>
<button type="button" onclick="myPrompt()">Find Integer</button>
<p id="demo_a"></p>
<p id="demo_b"></p>
<p id="demo_c"></p>
<p id="demo_d"></p>