JavaScript 2022

Term 3 Wednesday

May - July 2022

This site will be updated live during the sessions

Week 3 Exercise 3

		
001function getCars() {
		
002	 return [
		
003		{
		
004			doors:4,
		
005			colour:'red',
		
006			drive: function()
		
007			{
		
008				'Running 20mp'
		
009			}
		
010		},
		
011		{
		
012			doors:2,
		
013			colour:'blue',
		
014			drive: function()
		
015			{
		
016				'Running 20mp'
		
017			}
		
018		},
		
019		{
		
020			doors:5,
		
021			colour:'yellow',
		
022			drive: function()
		
023			{
		
024				'Running 20mp'
		
025			}
		
026		}
		
027	];
		
028};
		
029
		
030function displayCar(CARS)
		
031{
		
032	//declare an output string to store values from within found object
		
033	let carObjectsAsString = ''; 
		
034	// loop through array asigning each object to car
		
035	CARS.forEach(function(car)
		
036	{
		
037		// compare object colour value to yellow 
		
038		if(car.colour == 'yellow')
		
039			// loop around found object to append object data to output string
		
040			for(let carData in car)
		
041			{
		
042				// add data to output string
		
043				carObjectsAsString+=carData +': ' + car[carData] + '\n';	
		
044			}
		
045	});
		
046	//log output string to the console
		
047	console.log(carObjectsAsString);		
		
048}
		
049
		
050window.onload = function(){
		
051	const CARS = getCars();
		
052	// getting yellow car using the filter method
		
053	const yellowCar = CARS.filter(car => car.colour == 'yellow');
		
054	displayCar(CARS);
		
055}
		
056
		
057