Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## if statement question

The one parameter is the make of a car.

You are given two arrays, make and price, which exist - do not create them - just use them. The number of items in each array is the same.

Explanation:

make[2] holds the make of the 3rd car in the array.

price[2] is the price of that same car.

Calculate and return the total price for all cars where the make matches the parameter. This will be the total for all cars of a certain make.

TIPS:

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.

Example: if (make[...]==parameter1) { Replace ... with your loop index.

Remember: the if() goes inside the for(). The if() is indented inside the for().

3. When the make of a car matches the parameter add that price to the total. So to continue tip 2:

if (....) {

// add the price to total here

};

4. Return the total AFTER the loop ends. No inside AFTER the for();

-------------------------------------------------------------------------

This is what i have but its not working and i cant figure out why

function totalByColor(parameter1) {

total = 0;

for(i=0;i<price.length;i++){

if(make[i]==parameter1){

//this is a if statement for the price

total = total + price[i];

};

};

//the total is now received and can be returned

};

//the code is now over for this question

2. What are you using for the price and make arrays - that code should work if the arrays are defined at the time the function is run.

3. Originally Posted by completedjon
This is what i have but its not working and i cant figure out why

function totalByColor(parameter1) {

total = 0;

for(i=0;i<price.length;i++){

if(make[i]==parameter1){

//this is a if statement for the price

total = total + price[i];

};

};

//the total is now received and can be returned

};
As felgall indicated, your code should work given `make` and `price` arrays and assuming that `price` is an array of numbers and not an array of strings like `["\$20K", "\$25K", "\$30K"]`. If it is an array of strings, you may get output like `"0\$20K"`, `"0\$25K"`, and `"0\$30K"`.

You should declare your variables though. Putting `"use strict";` at the top of your code will force JavaScript into strict mode which will force you to do this in case you're forgetful. Adding `total` to `price[i]` is also pointless since `total = price[i]` will do the same thing.

Example of your code working with the above fixed and a few name and syntax changes:
Code:
```<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Demo</title>
<body>
<script>
(function () {
"use strict";
var makes = ["Chevrolet", "Hyundai", "Toyota"];
var prices = [20000, 25000, 30000];
function getPrice(make) {
var total = null;
var i = null;
for(i = 0; i < prices.length; i += 1){
if (makes[i] === make){
total = prices[i];
}
}
}
})();
</script>
</body>
</html>```

4. Thanks for the help, im in this class and theres limited chances to test these functions. this assignment has 5 questions and each "test" costs and attempt on the assignment.

if theres any advice on where i can go to ask simple question or test functions easier that would be great, im sure other people have more advanced questions on this forum than me so i dont want to take up too much space

5. Originally Posted by completedjon
if theres any advice on where i can go to ask simple question or test functions easier that would be great, im sure other people have more advanced questions on this forum than me so i dont want to take up too much space
Download a text editor like Sublime Text 2, copy or type your code into it, save the file with an *.html file extension, and double-click the file to open it in a browser.

You can tell if there are errors when your script doesn't work or the console shows errors. Different browsers have different methods of accessing the console. On Windows, you'd use Ctrl+Shift+K (Firefox), Ctrl+I (Chrome, Firefox (indirectly), and Opera), or F12 (Chrome and Internet Explorer).

I wouldn't worry about "tak[ing] up too much space". Sometimes it seems like there are too many answerers and not enough questioners.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•