Simple Array Question on LOOPS BUT i'm mostly NEW to this


#1

I’ve been handed a task by my supervisor to test my skills, the task he assigned to me was to;

  1. Create an array of vehicle Manufacturers (Minimum of 3).

  2. For each vehicle Manufacturers, create an array of (minimum of 3) vehicle models.

  3. Write a loop that will list each manufacturers and their corresponding model.

  4. Write out in numbers:
    –The total number of manufacturers.
    –The total number of models.
    –The total number of models per manufacturers.

  5. If the number of manufacturers is greater than or equal to (5), it should be listed as a “GRADE A” manufacturer.

  6. If the number of manufacturers is less than (5), it should be listed as a “GRADE B” manufacturer.

i have managed to get to this point,
let vecManufacturers= [‘Honda’, ‘Toyota’, ‘Hummer’, ‘BMW’, ‘Benz’];

let vecModels1= [‘Civic’, ‘Accord’, ‘Jazz’, ‘City’];
let vecModels2= [‘Hilux’, ‘Venza’, ‘Corolla’, ‘Camry’];
let vecModels3= [‘H1’, ‘H2’, ‘H3’, ‘H4’];
let vecModels4= [‘X1’, ‘X3’, ‘X5’, ‘6Series’, ‘7Series’];
let vecModels5= [‘Cclass’, ‘Sls’, ‘Glk’, ‘Slr’, ‘Cla’,‘GLA’];
let arrVehModels = [vecModels1, vecModels2, vecModels3, vecModels4, vecModels5];

for (let i = 0; i < vecManufacturers.length; i++) {
let manufacturer = vecManufacturers[i];
console.log(manufacturer);

let manuModels = arrVehModels[i];
for (let m = 0; m < manuModels.length; m++) {
var model = manuModels[m];
console.log(manufacturer, model);
}
};

writing the loop for the Grading has proven impossible, its been a serious case of writers block for days know! NEED HELP!!


#4

Hi @systemslayer21401,

I’m interested in the topic you posted, but first may I know is this a homework in school or?

I’ve written out a workable code myself, Codecademy guideline does not encourage blatantly posting the full working code as the answer. But I think I’m able to assist you in getting the end result.


You’re getting quite close actually, few hints that I could provide now are (according to my own code, everyone codes differently):

  • You need to declare 3 variables of counter, each keeps track of different situations:

    • 1 for total number of manufacturers. (var A)
    • 1 for total number of models. (var B)
    • 1 for total number of models for the manufacturer loop that you’re running. (var C)
      Notes: I simplified the counter names to A, B, C just for shorter reply.
  • The placement of these variables is important, if not, you will be counting wrongly.

    • var A and var B should be declared first before any loops start, so they should be at the very top, because you’d want to start counting as the loops goes. They start off with assigned value 0.
  • Same goes for var C, assign value 0, but this, you need to place in before looping the models of each manufacturer. For it to be restarted as 0 for every new manufacturer.

  • After declaring A, B, C with the correct placement, you need to insert codes to each counter to increase the value of A, B, C if the loop is being run.

    • For example, the loop for vecManufacturers will run for 5 times, therefore var A will be increased each time with the code A++, in the end after the loop finishes, A = 5, which is the total number of manufacturers based on how many data the Manufacturers’s array.
  • The placement of A++, B++, C++ is different from where you declared them in the first place, these code should be inside the running loop, so they can be increased each time loop is running, whereas, declaration should be outside the loop so that they are not always reset to 0 again and again.

  • So now, it’s time to test the code and see what is returning, you should construct your own console.log() to see what the loop is doing and print out the counters to make sure it’s counting correctly.

    • For my own test, I have console.log in every code blocks, which means inside the loop, after the loop etc to print out every manufacturers and every models together with the total number for each categories.
  • Lastly, with all the counters correctly coded, then you can proceed to the IF/ELSE statement which should be inserted after the loops, because now you have the counted value.

    Hint: you only need var C to do this.
    Check if var C >= 5:

    //I declared a new var manufacturerGrade, you may or may not do this:
    if (C >= 5) { 
       //    manufacturerGrade = "GRADE A";
       //    console.log(manufacturerGrade);
    } else {  
       //code 
    }
    

Sorry for the long post, you’re getting quite near, keep going, I hope this helps. Cheers! :slight_smile:

Side Note: In case you have future questions, it is much appreciated if you can follow through how to format code here. (I went through great deal to edit your code to test it out, just because how this forum messed it up without proper markup.)


#5

Thanks alot!!!
Not homework actually…more like Workwork! #FitTheNewGuySomeWhere


#6

@systemslayer21401,

You’re welcome :slight_smile:

Do try out the things I mentioned, if you still have questions or facing problems in your code, you’re more than welcome to post the questions with the latest code you have (format it in correct markup) so I can provide more explanation/assist to you.

Cheers.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.