[英]Creating a function that checks objects
I'm having trouble creating a function that will check the objects within my array.我在创建一个函数来检查数组中的对象时遇到问题。 It needs to check whether the year is 2018 or older and if it is, I need to output it.
它需要检查年份是否是 2018 年或更早,如果是,我需要输出它。 I have tried simpler methods that I thought would work like this:
我尝试了更简单的方法,我认为可以这样工作:
function checkQualifies(year){
year >= 2018
};
and function checkQualifies(item, index){ if(carTypes) { printCarTypes(carTypes.filter(x=>x.year>="2018")) } }
but it either breaks my code or won't run.和
function checkQualifies(item, index){ if(carTypes) { printCarTypes(carTypes.filter(x=>x.year>="2018")) } }
但它要么破坏我的代码,要么无法运行。 This is my whole code:这是我的全部代码:
<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>'
You forgot to call the checkQualifies()
function.您忘记调用
checkQualifies()
函数。 I called it just before the "Complete" line, tested and it works.我在“完成”行之前调用了它,经过测试并且可以正常工作。
<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.