簡體   English   中英

創建檢查對象的函數

[英]Creating a function that checks objects

我在創建一個函數來檢查數組中的對象時遇到問題。 它需要檢查年份是否是 2018 年或更早,如果是,我需要輸出它。 我嘗試了更簡單的方法,我認為可以這樣工作:

function checkQualifies(year){
    year >= 2018
  };

function checkQualifies(item, index){ if(carTypes) { printCarTypes(carTypes.filter(x=>x.year>="2018")) } }但它要么破壞我的代碼,要么無法運行。 這是我的全部代碼:

<html>
<head>
  <title>Assignment Seven : Array, Objects, Functions, Methods</title>
  <meta author="My Name" />
</head>
<body>
  <h1>Javascript Assignment</h1>

  <br/>

  <h2>Your Output</h2>

  <div id="my_output"></div>


</body>
</html>

<script>

  var studentName = "Alicia Villatoro";

  writeToPage("Assignment for: " + studentName);
  writeToPage("");

  writeToPage("Program 1: Writing an Array of Car Makes");
  writeToPage("");

  writeToPage("Creating Array: ");

  var array_name = car_makes;
  var car_makes = ["Toyota", " Honda", " Audi", " BMW", " Mercedes"];

  writeToPage("Displaying Array: ");

  writeToPage(car_makes);

  writeToPage("Program 2");
  writeToPage("");

  writeToPage("Create Car Object: ");

  var toyotaCamryCar = new Object();
  toyotaCamryCar['make'] = "Toyota";
  toyotaCamryCar['model'] = "Camry";
  toyotaCamryCar['year'] = "2019";
  toyotaCamryCar['price'] = 25000;

  writeToPage("Write the Car Object and Attributes");

  writeToPage(toyotaCamryCar['make']);
  writeToPage(toyotaCamryCar['model']);
  writeToPage(toyotaCamryCar['year']);
  writeToPage(toyotaCamryCar['price']);

  writeToPage("Program 3");
  writeToPage("");

  writeToPage("Create an Array of Objects");

  var carTypes = [];
  carTypes.push({make:"Toyota", model: "Camry", year: "2018"});
  carTypes.push({make: "Toyota", model: "Corolla", year: "2019"});
  carTypes.push({make: "Audi", model: "A4", year: "2017"});
  carTypes.push({make: "BMW", model: "i3", year: "2018"});
  carTypes.push({make: "Honda", model :"Accord", year: "2016"});

  writeToPage("Displaying Objects: ");

  printCarTypes(carTypes);

  writeToPage("Program 4");
  writeToPage("");

  writeToPage("Check the objects and print out the Toyota ones, use conditionals");

  if(carTypes){
    printCarTypes(carTypes.filter(x=>x.make==="Toyota"))
  };


  writeToPage("Program 5");
  writeToPage("");


  writeToPage("Create the checkQualifies function");

  function checkQualifies(item, index){
    if(carTypes)
    {
      printCarTypes(carTypes.filter(x=>x.year>="2018"))
    }
  }

  writeToPage("Run the checkQualifies below for each entry of the array from Program 3");


  writeToPage("Complete");


  function printCarTypes(carTypes) {
    for (var i = 0; i < carTypes.length; i++) {
      writeToPage("Car Types [" + i + "]: " + JSON.stringify(carTypes[i]));
    }
    writeToPage("");
  }
  function writeToPage(strText) {
    var output = document.getElementById("my_output");
    var currentValue = output.innerHTML;
    output.innerHTML = currentValue + "<br/>" + strText;
  }

</script>'

您忘記調用checkQualifies()函數。 我在“完成”行之前調用了它,經過測試並且可以正常工作。

 <html> <head> <title>Assignment Seven : Array, Objects, Functions, Methods</title> <meta author="My Name" /> </head> <body> <h1>Javascript Assignment</h1> <br/> <h2>Your Output</h2> <div id="my_output"></div> </body> </html> <script> var studentName = "Alicia Villatoro"; writeToPage("Assignment for: " + studentName); writeToPage(""); writeToPage("Program 1: Writing an Array of Car Makes"); writeToPage(""); writeToPage("Creating Array: "); var array_name = car_makes; var car_makes = ["Toyota", " Honda", " Audi", " BMW", " Mercedes"]; writeToPage("Displaying Array: "); writeToPage(car_makes); writeToPage("Program 2"); writeToPage(""); writeToPage("Create Car Object: "); var toyotaCamryCar = new Object(); toyotaCamryCar['make'] = "Toyota"; toyotaCamryCar['model'] = "Camry"; toyotaCamryCar['year'] = "2019"; toyotaCamryCar['price'] = 25000; writeToPage("Write the Car Object and Attributes"); writeToPage(toyotaCamryCar['make']); writeToPage(toyotaCamryCar['model']); writeToPage(toyotaCamryCar['year']); writeToPage(toyotaCamryCar['price']); writeToPage("Program 3"); writeToPage(""); writeToPage("Create an Array of Objects"); var carTypes = []; carTypes.push({make:"Toyota", model: "Camry", year: "2018"}); carTypes.push({make: "Toyota", model: "Corolla", year: "2019"}); carTypes.push({make: "Audi", model: "A4", year: "2017"}); carTypes.push({make: "BMW", model: "i3", year: "2018"}); carTypes.push({make: "Honda", model :"Accord", year: "2016"}); writeToPage("Displaying Objects: "); printCarTypes(carTypes); writeToPage("Program 4"); writeToPage(""); writeToPage("Check the objects and print out the Toyota ones, use conditionals"); if(carTypes){ printCarTypes(carTypes.filter(x=>x.make==="Toyota")) }; writeToPage("Program 5"); writeToPage(""); writeToPage("Create the checkQualifies function"); function checkQualifies(item, index){ if(carTypes) { printCarTypes(carTypes.filter(x=>x.year>="2018")) } } writeToPage("Run the checkQualifies below for each entry of the array from Program 3"); checkQualifies() writeToPage("Complete"); function printCarTypes(carTypes) { for (var i = 0; i < carTypes.length; i++) { writeToPage("Car Types [" + i + "]: " + JSON.stringify(carTypes[i])); } writeToPage(""); } function writeToPage(strText) { var output = document.getElementById("my_output"); var currentValue = output.innerHTML; output.innerHTML = currentValue + "<br/>" + strText; } </script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM