SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2012
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile Need help using "arguments" to capture multiple elements to add all of the prices

    Code JavaScript:
    var Games = function () { };
    Games.prototype.store = {
        _gameTitle: ['Killzone 2', 'Halo 3', 'Uncharted 3', 'Gran Turismo 5', 'Modern Warfare 2', 'Final Fantasy XIII-2'], //List of games will be used
        _prices: [34.01, 17.99, 23.99, 29.37, 24.34, 21.81], //Prices for the "_gameTitle"
     
        searchGame: function (searchGames) { //The searchGame() function will be used to find the "_gameTitle" property.
     
            var gameMAX = this._gameTitle.length; //Cache the length of the "_gameTitle" property.
     
            for (var i = 0; i < gameMAX; i++) { //I will be using a for-loop to loop through the length of "gameMAX".
     
                if (searchGames === this._gameTitle[i]) { //Checks if searchGames matches any elements within the "_gameTitle" array property.
     
                    return searchGames + ' is found'; //Will return true.
                }
            }
            return searchGames + ' is not found'; //Otherwise the value will return false.
        },
        findPrice: function (getPrice) {
     
            //priceMAX and gameMAX will be used to cache data.
            var priceMAX = this._prices.length,
                gameMAX = this._gameTitle.length;
     
            //Will be looping through the length of _prices.length to compare the price.
            for (var x = 0; x < priceMAX; x++) {
                if (getPrice === this._gameTitle[x]) { //Checks if getPrice matches any elements within the "_gameTitle" array property.
                    return 'The price for ' + getPrice + ' is: ' + this._prices[x]; //Statement will be true.
                }
            }
            return getPrice + ' is not found'; //Otherwise the value will be false.
        },
        buyGames: function () {
            //Prepared the variables to be used in this method.
            var args = arguments.length,
                total = 0,
                games = this._gameTitle,
                prices = this._prices,
                a = 0;
     
     
            /*This is where I'm having difficulties trying to use multiple game titles to add up all of the prices for them by using 
              arguments as my solution.
            */
            for (; a < games.length; a++) { //Loop through the length of games property.
                for (var x = 0; x < args; x++) { 
                    if (arguments[x] === games[x]) {
                        arguments[x] = prices[x];
                        total += arguments[x];
                    }
                }
            }
            return total;
        }
    }
     
    var result = new Games();
    result.store.buyGames('Killzone 2');

    The code inside "buyGames" method only recognize the first element inside the "games" array. But won't let me add up the total price with the other elements. I'm having difficulties trying to concatenate my "total" with the number of elements inside the "prices" array.

    I want to have the ability to pick the "games" I want and let the "total" variable add up the cost.

    Much appreciated,

    Thanks

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by shurns View Post
    The code inside "buyGames" method only recognize the first element inside the "games" array.
    You're going to want to use two different loops. An outer loop that loops through each of the arguments, and an inner loop that loops through the available games checking for a match.
    Last edited by paul_wilkins; Mar 7, 2012 at 22:13.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2012
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works now. Thanks


Tags for this Thread

Bookmarks

Posting Permissions

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