Steps for incorporating code to calculate the total price and append it to the orderMessage

I am seeking help with this program that my professor assigned to me. The instructions marked by "//" are the ones I need to implement in the code, but I'm struggling to understand how to proceed. Any assistance would be greatly appreciated, even just a starting point. I've attempted a few solutions, but none of them have worked so far.

<!DOCTYPE html>
<html lang="en-US"> 
    <head>
    <title>JavaScript Project T-Shirt Order</title>
    <script>
     /*
     *  Function OrderTShirt
     *
     *  Inputs:
     *     selectedColor - initialized from the select tag with id="colorChoice"
     *     selectedStyle - initialized from the select tag with id="style"
     *     selectedSize  - initialized from the select tag with id="size"
     *
     *  Output:
     *      alert of the order message
     *
     */
     function OrderTShirt(){

       //initialize the input
       var selectedColor = document.getElementById("colorChoice").value;
       var selectedStyle = document.getElementById("style").value;
       var selectedSize =  document.getElementById("size").value;


       //create the orderMessage to alert - you may want to move/change this code
       var orderMessage = "Your order is:  Color = " + selectedColor 
                      + "; Style = " + selectedStyle 
                      + "; Size = " + selectedSize+"\n";


       //Add code to calculate the price and concatenate it to the orderMessage
       var price = document.getElementById()
       //For example, "The order is:  Color = gray; Style=T-Shirt; Size = XL \nThe price is $7" 

       //If the style and size are not available, then concatenate the message that 
       // the selection is not available instead of a price.

       // a t-shirt in any size is $7 except 2XL which is $10.
       // a v-neck in adult sizes is $12.50 except the 2XL is not available
       // a tech shirt in adult sizes is $13

       alert(orderMessage);
   }

    </script>
    </head>

    <body>
    <p>
    <table>
      <tr/>
      <tr><th>Colors</th><th>Styles</th><th>Sizes</th></tr>
      <tr><td>
    <select id="colorChoice">
      <option value="gray">Gray</option>
      <option value="gold">Gold</option>
      <option value="red">Red</option>
      <option value="pink">Pink</option>
      <option value="white">White</option>
      <option value="navy">Navy</option>
      <option value="maroon">Maroon</option>
      <option value="black">Black</option>
      <option value="royalblue">Royal Blue</option>
      <option value="limegreen">Lime Green</option>
    <select>
       </td><td>
    <select id="style">
      <option value="T-Shirt">T-Shirt</option>
      <option value="Tech Shirt">Moisture Wicking Tech Shirt</option>
      <option value="Ladies V-Neck">Ladies V-Neck - Adult sizes only</option>
    </select>
      </td><td>
    <select id="size">
      <option value="XS">Adult Extra Small</option>
      <option value="S">Adult Small</option>
      <option value="M">Adult Medium</option>
      <option value="L">Adult Large</option>
      <option value="XL">Adult Extra Large</option>
      <option value="2XL">Adult 2X</option>
    </select>
      </td></tr>
      <tr/><tr><td>
          <input type="button" 
                 value="Order T-Shirt" 
                 onclick="OrderTShirt();">
       </td><td/><td/>
    </table>
    <p>
    </body>
</html>

Answer №1

Check out this code snippet, it seems to address your needs perfectly. I've added a new function named getPrice which is responsible for calculating the price based on the shirt style and size. If the requested size is unavailable, it will return "Unavailable".

<!DOCTYPE html>
<html lang="en-US> 
    <head>
    <title>JavaScript Project T-Shirt Order</title>
    <script>
     /*
     *  Function OrderTShirt
     *
     *  Inputs:
     *     selectedColor - obtained from select tag with id="colorChoice"
     *     selectedStyle - obtained from select tag with id="style"
     *     selectedSize  - obtained from select tag with id="size"
     *
     *  Output:
     *      Alert message displaying the order details
     *
     */
     function OrderTShirt(){

       //initialize the input
       var selectedColor = document.getElementById("colorChoice").value;
       var selectedStyle = document.getElementById("style").value;
       var selectedSize =  document.getElementById("size").value;


       //create the orderMessage for alert - you may want to move/change this code
       var orderMessage = "Your order: Color = " + selectedColor 
                      + "; Style = " + selectedStyle 
                      + "; Size = " + selectedSize+"\n";


       //Add code to compute the price and add it to the orderMessage
       var price = getPrice(selectedColor, selectedStyle, selectedSize);

       if(price === "Unavailable") {
           orderMessage += "This shirt is not available";
       } else {
           orderMessage += "Price = " + price;
       }
       

       alert(orderMessage);
   }

    function getPrice(color, style, size) {
        var price;

        if(style === "T-Shirt") {
            if(size === "2XL") {
                price = 10;
            }else {
                price = 7;
            }
        }
        else if(style === "Ladies V-Neck") {
            if(size === "2XL") {
                // Unavailable
                price = "Unavailable"
            }else {
                price = 12.50;
            }
        }
        else if(style === "Tech Shirt") {
            price = 13;
        }

        return price;
    }

    </script>
    </head>

    <body>
        <table>
            <tr>
                <th>Colors</th>
                <th>Styles</th>
                <th>Sizes</th>
            </tr>
            <tr>
                <td>
                    <select id="colorChoice">
                        <option value="gray">Gray</option>
                        <option value="gold">Gold</option>
                        <option value="red">Red</option>
                        <option value="pink">Pink</option>
                        <option value="white">White</option>
                        <option value="navy">Navy</option>
                        <option value="maroon">Maroon</option>
                        <option value="black">Black</option>
                        <option value="royalblue">Royal Blue</option>
                        <option value="limegreen">Lime Green</option>
                    </select>
                </td>
                <td>
                    <select id="style">
                        <option value="T-Shirt">T-Shirt</option>
                        <option value="Tech Shirt">Moisture Wicking Tech Shirt</option>
                        <option value="Ladies V-Neck">Ladies V-Neck - Adult sizes only</option>
                    </select>
                </td>
                <td>
                    <select id="size">
                        <option value="XS">Adult Extra Small</option>
                        <option value="S">Adult Small</option>
                        <option value="M">Adult Medium</option>
                        <option value="L">Adult Large</option>
                        <option value="XL">Adult Extra Large</option>
                        <option value="2XL">Adult 2X</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    <button onclick="OrderTShirt();">Order T-Shirt</button>
                </td>
            </tr>
        </table>
    </body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Disable the button until all input fields contain text in ASP

Curious if anyone knows how to disable a button until all text boxes have input in ASP.NET and C#. Here is an image showing the scenario I'm referring to - wanting to gray out the commit button. Thanks, Chris! ...

React DataGrid fails to refresh when state changes

Currently, I am in the process of developing a link tree using the Datagrid Component provided by MaterialUI. While data can be successfully displayed, I encounter an issue when attempting to add a new entry. The problem lies in the fact that despite cha ...

Play button icon is missing in Firefox when using absolute positioning

I'm currently experiencing an issue with my video gallery section. I have both normal and hover versions of a play button positioned above a preview image of a video. However, the play buttons are not visible at all in Firefox. You can view the page h ...

The function of slidetoggle is malfunctioning when clicked

I currently have two dynamic containers with one displaying grouped boxes, quantities, and totals, while the other shows detailed information. Both container values are added dynamically at runtime. By default, the grouping container is displayed on the p ...

Can you explain the distinction between angular input and native HTML attributes when used with HTML tags?

Take the input tag as an example, where you have two options to specify a maximum length: <input [maxLength]="20" type="text"> <input maxLength="20" type="text"> Are there any real-world distinctions between using one approach over the othe ...

Is there a way to automatically hide a div based on a checkbox value when the page loads?

How can I hide a div in my view when a checkbox is not checked upon page load? <input type="checkbox" class="k-checkbox" id="chkInvoiceStatusActive" asp-for="InvoiceStatus" value="true" /> <input t ...

What are the steps to refreshing a table using AJAX?

Struggling to update a table from my database, I have been following a PHP guide but can't get it to work. In a separate file, the data is retrieved and displayed in a table. I am attempting to use JavaScript to refresh this file. This code snippet ...

Retaining previous values in Angular reactive form during the (change) event callback

Imagine having an Angular reactive form with an input field. The goal is to keep track of the old value whenever the input changes and display it somewhere on the page. Below is a code snippet that achieves this functionality: @Component({ selector: & ...

Tips for transferring data from a JavaScript page to a C# page by utilizing Jquery

I am working with a dynamically created table that contains values in its selected rows. I need to extract all td.innerText values from each selected row and send them to a C# page, but I am unsure of the best approach. I attempted to use JSON, but I am ...

RobotFramework encounters difficulty locating an element using JavaScript

After working with RF for the past few weeks, I came across a persistent issue that has been bothering me. I keep getting the following error: The element with the locator 'XXX' (just a template) cannot be found. Upon investigating the span tha ...

What is the process for enabling HTMLField in Django Admin and ensuring it is accurately displayed on the template?

One of the models in my Django app is for dorms and it looks like this: class Dorm(models.Model): dorm_name = models.CharField(max_length=50, help_text="Enter dorm name") dorm_description = models.TextField(max_length=1000, help_text="Enter dorm d ...

Having trouble with the page redirection issue? Here's how you can troubleshoot and resolve

My goal is to restrict access to both the user home and admin dashboard, allowing access only when logged in. Otherwise, I want to redirect users to the login or admin login page. import { NextResponse } from 'next/server' import { NextRequest } ...

Why does i18next only function on http://localhost:3000/de (landing page) in NextJS and not on http://localhost:3000/de/about?

I'm new to this and feeling lost on how to tackle this issue. I tried following the instructions on https://github.com/i18next/next-i18next, but I'm struggling with index.js. When I toggle /de on my landing page, it translates correctly in the UR ...

How can I merge these two Observables in Angular to create an array of objects?

Let's say we are working with two different datasets: student$ = from([ {id: 1, name: "Alex"}, {id: 2, name: "Marry"}, ]) address$ = from([ {id: 1, location: "Chicago", sid: 1}, {id: 2, location: &qu ...

What is the best way to add a class to the initial HTML element in my specific scenario?

Currently utilizing the angular framework in my application. Desire to assign a specific class to only the initial element within my ng-repeat iteration. <div class='initialOnly' ng-repeat = 'task in tasks'>{{task.chore}}</di ...

The app was rejected from the Play Store due to a breach of section 4.4 in the Developer Distribution Agreement

Just got an email notification that my application submission, Chami Browser, has been rejected due to violation of section 4.4 of the Developer Distribution Agreement. The reason for rejection is accessing YouTube videos in violation of their Terms of Se ...

How to ensure an absolutely positioned div spans the entire width of the screen

I am facing an issue with an absolutely positioned div that I want to stretch the full width of the screen using 100vw. However, the problem is that it aligns with its parent's starting point rather than the left side of the viewport. How can I make s ...

Ways to track the visit data for different languages in Google Analytics when the language is not included in the URL

On my website, I track Google Analytics by adding the tracking code to the header. The standard implementation of GA tracking is as follows: ga('create', 'UA-483951-1', 'auto'); ga('send', 'page ...

Encountered an issue when attempting to create meanjs using yo

After installing yeoman globally, I encountered an error while trying to generate meanjs with yo. Unfortunately, I was unable to identify the root cause of the problem. view image here Any help would be greatly appreciated. I attempted using sudo as well ...

Is it possible to pass multiple parameters in one onClick event?

My search bar is located below. Upon clicking the 'search' button, I wish for it to update results and redirect to a different page simultaneously. function SearchBar(props) {   const [innerSearch, setInnerSearch] = useState(""); ...