- This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
Viewing 3 posts - 1 through 3 (of 3 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › 2 arrays – printing when they match up
function totalByColor(carMake) {
make=new Array();
make[0] = ‘ford’;
make[1] = ‘honda’;
make[2] = ‘chevy’;
make[3] = ‘toyota’;
price=new Array();
price[0] = ‘100’;
price[1] = ‘200’;
price[2] = ‘300’;
price[3] = ‘400’;
total = 0;
for (i=0; i=make.length; i++) {
if (carMake = price) {
return make + price;
}
};
};
Okay, so I’m helping a friend with some javascript homework, which I am in no way any good at. But the basis is to run a for loop, when the parameter carMake is “imputed” it will look at both arrays, then return the make and the price. I figured it would have to somehow running the loop to just look at the subscripts and then print out the two that match each other.
Some other info: the arrays I setup are for testing. In the directions he already has all the arrays setup. And under his “tips” he has: 1. You need a for loop that repeats for the length of the array (either one). 2. Inside the loop you need an if that determines when the make array matches the parameter. 3. When the make of a car matches the parameter add that price to the total.
I’m sure this is java 101 for absolute beginers, but I’m not sure how to go about it correctly. I had helped with 4 other problems that seemed a bit easier then this, just not sure where to start from what I have.
I don’t have time to play with this now. But in your if statement you are assigning price to carMake, it should be `==` or better yet `===`.
var priceMyCar = function(car){
var carMake = ;
var carPrice = [500,100,5];
for (var i = 0; i < carMake.length; i++) {
if (carMake === car){
var index = carMake.indexOf(car);
}
if (typeof carPrice[index] != ‘undefined’){
return carPrice[index];
} else{
return ‘No Match’;
}
}
// Call the function passing in argument
console.log(priceMyCar(‘Audi’)); // 500
Hope that helps.