Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

    return total;

    };

    //the code is now over for this question

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,616
    Thanks
    0
    Thanked 645 Times in 635 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by completedjon View Post
    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

    return total;

    };
    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">
    	<head>
    		<meta charset="utf-8">
    		<title>Demo</title>
    	</head>
    	<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];
    						}
    					}
    					return total;
    				}
    				alert(getPrice("Toyota"));
    			})();
    		</script>
    	</body>
    </html>
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #4
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by completedjon View Post
    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.
    For every complex problem, there is an answer that is clear, simple, and wrong.


  •  

    Posting Permissions

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