Forums

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

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #45847
    Jeager
    Member

    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.

    #140447
    Alen
    Participant

    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 `===`.

    #140466
    Alen
    Participant

    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.

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.