顺序搜索不能在javascript数组中工作

时间:2015-07-04 11:23:18

标签: javascript algorithm

我一直试图解决这个问题好几天,但我似乎无法。也许有人可以为我看一下我的代码并提出一些想法。

var products = [
["Test Product", "Test Manufacturer", "Test Model", "Test Class","Test Sellingprice","Test Costprice","Test Quantity"]
]

function createProduct() {
    var name = document.getElementById("productname").value
    var myclass = document.getElementById("productclass").value
    var model = document.getElementById("model").value
    var manufacturer = document.getElementById("manufacturer").value
    var sellingprice = document.getElementById("productsellprice").value
    var costprice = document.getElementById("productcostprice").value
    var quantity = document.getElementById("quantity").value
    productobject =[name, manufacturer, model, myclass,sellingprice,costprice,quantity];
    products.push(productobject);

}

function sequentialsearch() {
    var searchvalue = document.getElementById("search").value;
    for (i = 0; i < products.length; i++) {
        for (j = 0; j < products[i].length; j++) {
            products[i][j].toString;
            searchvalue.toString;
            if (searchvalue == products[i][j]) {
                document.getElementById("searchresults").innerHTML = products[j]
            }
            else if (products[i][j] != searchvalue) {
                document.getElementById("searchresults").innerHTML = "Not Found!!"
            }
        }
    }
}

搜索继续给予&#34;未找到&#34;即使产品就在阵列中

function noduplicate() {
    for (i = 0; i < products.length;i++) {
        if (productobject == products[i]) { document.getElementById().innerHTML = "This Product Already Exists!!" }
            products.pop(productobject)
    }
}

function noduplicate()即使有完全相同的内容也找不到重复内容。

有人能指出我可能阻止正确执行这两种功能的错误吗?

1 个答案:

答案 0 :(得分:0)

你在这里,在sequentialsearch函数中有一些错误,所以我重构它:

&#13;
&#13;
var products = [
  ["Test Product", "Test Manufacturer", "Test Model", "Test Class", "Test Sellingprice", "Test Costprice", "Test Quantity"],
  ["Test Product1", "Test Manufacturer", "Test Model", "Test Class", "Test Sellingprice", "Test Costprice", "Test Quantity"]
];
//alert(products[0][0]);
function createProduct() {
  var name = document.getElementById("productname").value;
  var myclass = document.getElementById("productclass").value;
  var model = document.getElementById("model").value;
  var manufacturer = document.getElementById("manufacturer").value;
  var sellingprice = document.getElementById("productsellprice").value;
  var costprice = document.getElementById("productcostprice").value;
  var quantity = document.getElementById("quantity").value;
  productobject = [name, manufacturer, model, myclass, sellingprice, costprice, quantity];
  products.push(productobject);
};
// THIS FUNCTION GOT ERROR
function sequentialsearch() {
  document.getElementById("searchresults").innerText = '';
  var searchvalue = document.getElementById("search").value;
  for (i = 0; i < products.length; i++) {
    for (j = 0; j < products[i].length; j++) {
      if (searchvalue == products[i][j]) {
        document.getElementById("searchresults").innerText += products[i][0] + ', ';
      }
    }
  }
  // HERE only if searchresults is null or empty
  if (!document.getElementById("searchresults").innerText) {
    document.getElementById("searchresults").innerText = "Not Found!!";
  }
};
&#13;
<input id="search" type="text" value="Test Product" />
<br />The result: <span id="searchresults"></span>

<br />
<input type="button" value="search" onclick="sequentialsearch()" />
&#13;
&#13;
&#13;

希望得到这个帮助。