www.webdeveloper.com
Results 1 to 5 of 5

Thread: Problems Calling two functions

  1. #1
    Join Date
    Feb 2011
    Posts
    3

    Problems Calling two functions

    I home school my daughter and am not familiar with javascript. She wrote two functions and then they are called in the body. But they are not doing as she intended...displaying the DishName and the DishDesc.

    I have spent all night trying to figure this one out, but I really don't know what is wrong with it. Maybe someone can help us out.

    Here is the code so far:

    <html>
    <head>
    <title>Kelsey's Dinner Menu</title>
    <script language="JavaScript">
    <!--- Hide from non-JavaScript browsers
    function DishName(Day) {
    var DName=new Array();
    DName[0]="Chicken Burrito Amigo";
    DName[1]="Chicken Tajine";
    DName[2]="Pizza Bella";
    DName[3]="Salmon Fillet";
    DName[4]="Greek-style Shrimp";
    DName[5]="All-you-can-eat fish";
    DName[6]="Prime Rib";
    return Day[DName];
    }
    function DishDesc(Day) {
    var DDesc=new Array();
    DDesc[0]="Chicken with mushrooms, onions, and Montery

    Jack cheese wrapped in a flour tortilla 9.95";
    DDesc[1]="Chicken baked with garlic, olives, capers,

    and prunes 8.95";
    DDesc[2]="Large pizza with pesto, goat cheese, onions,

    and mozzarella cheese. 8.95";
    DDesc[3]="Grilled salmon with a spicy curry sauce and

    baked potato. 9.95";
    DDesc[4]="Shrimp, feta cheese, and tomatoes simmered

    in basil and garlic. 9.95";
    DDesc[5]="Deep-fried cod with baked potato and rolls.

    9.95";
    DDesc[6]="12-oz cut with baked potato, rolls, and

    dinner salad. 12.95";
    return Day[DName];
    }
    // Stop hiding -->
    </script
    <style>
    body {background-image:url(tan.jpg)}
    h3 {color:blue}
    dt {font-weight:bold; color:green}
    </style>
    </head>


    <body>
    <center><img src="dinner.jpg">
    <h5><span style="font-size:x-large; color:green">
    Dinner Menu</span><br>
    Served 4:00 p.m. - 10:00 p.m.</h5><hr></center>
    <dl>
    <h3>Today's Special</h3>


    <dt>
    <script language="JavaScript">
    <!--- Start hiding from non-JavaScript browser
    //Insert the titles of the nightly specials below
    var Today=new Date();
    var WeekDay=Today.getDay();
    document.write(DishName(WeekDay));

    //Stop hiding -->
    </script>


    <dd>
    <script language="JavaScript">
    <!--- Start hiding from non-JavaScript browser
    //Insert the descriptions of the nightly specials below
    var Today=new Date();
    var WeekDay=Today.getDay();
    document.write(DishDesc(WeekDay));

    //Stop hiding -->
    </script>


    <dt><hr>Cajun Meatloaf
    <dd>Lean beef served with Kelsey's Southwestern mashed

    potatoes. 8.95
    <dt>Chicken Marsala
    <dd>Plump chicken breasts sauteed with tomatoes,

    mushrooms, & artichoke hearts
    with a splash of Marsala wine. 8.95
    <dt>Pecan Crusted Whitefish
    <dd>Quickly seared fish & fresh mushrooms served with

    pineapple chutney. 10.95
    <dt>Thai Stir Fry
    <dd>Fresh crisp vegetables quickly sauteed, tossed

    with Kelsey's spicy
    peanut sauce, served over rice. 8.95
    <dt>Jambalaya
    <dd>Plump shrimp, chicken, & spicy sausage slowly

    simmered with tomatoes and
    rice. 9.95
    <dt>Wild Rice Stuffed Squash
    <dd>Winter squash stuffed with fresh harvest

    vegetables & pecan rice, served
    with pineapple chutney & melted Monterey Jack.

    8.95
    <dt>Cuban Beans & Rice
    <dd>Black turtle beans simmered with carrots,

    tomatoes, & green peppers,
    served over rice with spicy salsa, guacamole, &

    flour tortillas. 7.95
    </dl>
    </body>

    </html>



    Thank you

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    I've cleaned up your code a bit. You had unclosed tags and there is still deprecated html code but it isn't affecting the way the javascript works so I haven't cleaned it out.

    I simplified your 2 functions into a single 2 dimensional array where each row in the array contains the dishname and its corresponding description.

    When the page loads the onload function loads the dishname for the day and its description into the appropriate elements.


    Code:
     
    <html>
        <head>
            <title>Kelsey's Dinner Menu</title>
            <script type="text/javascript">
                var dishData = new Array();
                dishData = [
                    ['Chicken Burrito Amigo','Chicken with mushrooms, onions, and Montery.<br />Jack cheese wrapped in a flour tortilla 9.95'],
                    ['Chicken Tajine','Chicken baked with garlic, olives, capers,and prunes 8.95'],
                    ['Pizza Bella','Large pizza with pesto, goat cheese, onions,and mozzarella cheese. 8.95'],
                    ['Salmon Fillet','Grilled salmon with a spicy curry sauce and baked potato. 9.95'],
                    ['Greek-style Shrimp','Shrimp, feta cheese, and tomatoes simmered in basil and garlic. 9.95'],
                    ['All-you-can-eat fish','Deep-fried cod with baked potato and rolls.  9.95'],
                    ['Prime Rib','12-oz cut with baked potato, rolls, and dinner salad. 12.95']
                ];
                window.onload=function(){
                    var Today=new Date();
                    var weekDay=Today.getDay();
                    document.getElementById('dtNightlySpecialName').innerHTML = dishData[weekDay-1][0];
                    document.getElementById('dtNightlySpecialDesc').innerHTML = dishData[weekDay-1][1];
                }
            </script>
        </head>
        <body>
            <center>
                <img src="dinner.jpg" alt="">
                <h1><span style="font-size:x-large; color:green">
                        Dinner Menu</span><br>
                    Served 4:00 p.m. - 10:00 p.m.</h1>
                <hr>
            </center>
            <h3>Today's Special</h3>
            <dl>
                <dt id="dtNightlySpecialName"></dt>
                <dd id="dtNightlySpecialDesc"></dd>
                <dt><hr>Cajun Meatloaf</dt>
                <dd>Lean beef served with Kelsey's Southwestern mashed potatoes. 8.95</dd>
                <dt>Chicken Marsala</dt>
                <dd>Plump chicken breasts sauteed with tomatoes,
                    mushrooms, & artichoke hearts
                    with a splash of Marsala wine. 8.95
                </dd>
                <dt>Pecan Crusted Whitefish</dt>
                <dd>Quickly seared fish & fresh mushrooms served with
                    pineapple chutney. 10.95
                </dd>
                <dt>Thai Stir Fry</dt>
                <dd>Fresh crisp vegetables quickly sauteed, tossed
                    with Kelsey's spicy
                    peanut sauce, served over rice. 8.95
                </dd>
                <dt>Jambalaya</dt>
                <dd>Plump shrimp, chicken, & spicy sausage slowly
                    simmered with tomatoes and
                    rice. 9.95
                </dd>
                <dt>Wild Rice Stuffed Squash</dt>
                <dd>Winter squash stuffed with fresh harvest
                    vegetables & pecan rice, served
                    with pineapple chutney & melted Monterey Jack.
                    8.95
                </dd>
                <dt>Cuban Beans & Rice</dt>
                <dd>Black turtle beans simmered with carrots,
                    tomatoes, & green peppers,
                    served over rice with spicy salsa, guacamole, &
                    flour tortillas. 7.95
                </dd>
            </dl>
        </body>
    </html>

  3. #3
    Join Date
    Feb 2011
    Posts
    3

    Still not understanding

    Since we are so new to this, the changes are not understood. I am sure you wrote it much better, but we don't understand what was wrong in the first place. Thank you for trying to help, we are just lost.

  4. #4
    Join Date
    Nov 2010
    Posts
    86
    It looks like there's a mistake when your functions are returning a value. For example in this function:
    Code:
    function DishName(Day) {
    var DName=new Array();
    DName[0]="Chicken Burrito Amigo";
    DName[1]="Chicken Tajine";
    DName[2]="Pizza Bella";
    DName[3]="Salmon Fillet";
    DName[4]="Greek-style Shrimp";
    DName[5]="All-you-can-eat fish";
    DName[6]="Prime Rib";
    return Day[DName];
    }
    You create an array called DName, and then at the end you say:
    Code:
    return Day[DName];
    which I think is backwards, and instead you want
    Code:
    return DName[Day];
    To get a value from an array you use:
    Code:
     array_name[index_number];
    For example if you want "Chicken Tajine", you would get it with
    Code:
    DName[1];
    Hope this helps.

  5. #5
    Join Date
    Feb 2011
    Posts
    3

    Thank you so much!

    Not only did it fix it, but now my daughter knows what her mistake is. Thank you so very much!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles