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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Should I use an array of object literals?

    I'm making a simple code that chooses a random fast food place from an array and then tells you if they are open or not. I was wondering what the simplest way to code this. If it would be an array of object literals, how should I organize it?
    Here is what I have so far:
    Code:
    <script type = "text/javascript">
    		function chooseRestaurant() {
    			var restaurant = new Array("Jack 'n the Box","Mcdonalds","Taco Bell","Dominos","Pizza Hut","Rally's","Hardee's","Cici's","Subway","East Coast Subs","Five Guy's","Wendy's","KFC","Burger King","Chick-fil-a","Lee's","Steak 'n Shake","Arby's","Papa John's","Long John Silver's","White Castle","Sonic");
    		
    			var random = restaurant[Math.floor(Math.random()*restaurant.length)];
    		
    			document.getElementById("choose").innerHTML=("Your random fast food selection is: " + random + "");
    			document.getElementById("time").innerHTML=Date();
    			}
    </script>
    </head>
    <body>
    	<h1 id="choose">Choose a Random Place to eat with the click of a button!</h1>
    	<p id="time"> </p>
    	<p id ="open"> </p>
    	<button onclick="chooseRestaurant() ">Click here to choose food!</button>
    	<button onclick="areOpen()">Are they open?</button>
    	<p>Click Refresh (or Reload) to run the script again</p>
    </body>

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,105
    Thanks
    23
    Thanked 594 Times in 593 Posts
    Because you said "easy" and it doesn't look like you are looking for 5 Mcdonalds I just add a couple of more arrays. Easy to add what I deleted.
    Code:
    <html>
    <head>
    <script type = "text/javascript">
    function chooseRestaurant() {
    	var restaurant = new Array("Jack 'n the Box","Mcdonalds","Taco Bell","Dominos","Pizza Hut");
    	var start = new Array("12am","8am","10am","2pm","11am");
    	var end = new Array("12pm","2am","11pm","12pm","11pm");
    	var name = Math.floor(Math.random()*restaurant.length);
    	var random = restaurant[name];
    	var starttime = start[name];
    	var endtime = end[name];
    	document.getElementById("choose").innerHTML=("Your random fast food selection is: " + random);
    	document.getElementById("cheese").innerHTML=("Opens "+starttime+" and stays open til "+endtime);
    }
    
    </script>
    </head>
    <body>
    	<h1 id="choose">Choose a Random Place to eat with the click of a button!</h1>
    	<h1 id="cheese"></h1>
    	<button onclick="chooseRestaurant() ">Click here to choose food!</button>
    	<p>Click Refresh (or Reload) to run the script again</p>
    </body>
    </html>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    Hell4u3 (04-29-2013)

  • #3
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    This is way easier than I had in mind. Thanks a lot. I kept looking at it and I guess I was over complicating it. That was a very easy solution.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,634
    Thanks
    0
    Thanked 649 Times in 639 Posts
    To improve the coding you could declare all the variables in a single var at the top of the function instead of where they are used so that the declaration is at the same spot where it actually runs.

    Also there is no need to specify new Array() as [] does the same thing with fewer keystrokes.
    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.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,216
    Thanks
    75
    Thanked 4,344 Times in 4,310 Posts
    Well, I have to disagree. With both solutions.

    I think this is the perfect example of the need for a constructor.

    Example:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>How to eat poorly</title>
    </head>
    <body>
    <div>
    	<h1 id="choose">Choose a Random Place to eat with the click of a button!</h1>
    	<h1 id="hours"></h1>
    	<button id="choice">Click here to choose food!</button>
    </div>
    
    <script type = "text/javascript">
    /* contructor for a restaurant */
    function Restaurant( rname, start, end )
    {
        this.name = rname;
        this.openAt = start;
        this.closeAt = end;
    }
    
    var restaurants = [
            new Restaurant("Jack 'n the Box","12am","12pm"), /* only open for breakfast? */
            new Restaurant("Mcdonalds","8am","2am"),
            new Restaurant("Taco Bell","10am","11pm"),
            new Restaurant("Dominos","2pm","12pm"), /*?? closed for lunch ??*/
            new Restaurant("Pizza Hut","11am","11pm")
        ];
    
    document.getElementById("choice").onclick = function( )
    {
        var r = restaurants[ Math.floor( Math.random() * restaurants.length ) ];
        document.getElementById("choose").innerHTML= "Your random fast food selection is: " + r.name;
        document.getElementById("hours").innerHTML= "Opens " + r.openAt + " and stays open til " + r.closeAt;
    }
    
    </script>
    
    </body>
    </html>
    It's much better organized than the parallel arrays (and allows you to quickly see nonsense such as the hours for Dominos). And it's much more compact than a series of object literals would be. But it gives you all the advantages of a bunch of object literals, too.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    Quote Originally Posted by Hell4u3 View Post
    I was wondering what the simplest way to code this. If it would be an array of object literals, how should I organize it?
    regarding the question whether you should use an Array or an Object for something, simply ask yourself: Do I need Array functionality (loops, array modification, etc.) or none at all? plain Objects can be used as simple data storage, but that’s pretty much it.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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