Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

I have a JavaScript issue?

I'm working the examples in my textbook (and I don't have the data files with the correct format) but I can't seem to get my output to show correctly.

The issue is that it's not concatenating the customer name and displaying the lower information after computing the cost.

// Welcome the user

// Ask for ordering information

document.write("Welcome to Bulk Is Best!" + BR);

firstName = prompt("Enter your name",ES);

lastName = prompt("Enter your last name",ES);

numPkgs = prompt("Enter number of packages",ES);

state = prompt("State to ship to",ES);

// Concatentate first and last name

custName = firstName + " " + lastName;

// Compute per package price and shipping cost

if (numPkgs >= 6) {

pkgPrice = "2.00";

}

else {

pkgPrice = "2.50";

}

if (state == "ID") {

shipCost = "0.00";

}

else {

shipCost = "4.00";

}

// Compute and document.write order totals

totPkgCost = pkgCost * numPkgs;

totOrderCost = totPkgCost + shipCost;

document.write("Order Summary for " + custName + BR);

document.write("Number packages ordered: " + numPkgs + BR);

document.write("Price per package: " + pkgPrice + BR);

document.write("Subtotal: " + totPkgCost + BR);

document.write("Order will be shipped to: " + state + BR);

document.write("Shipping cost: " + shipCost + BR);

document.write("Order total: " + totOrderCost + BR);

Update:

I couldn't fit the entire code in.

// Declare variables

var numPkgs;

var pkgPrice;

var totPkgCost;

var shipCost;

var totOrderCost;

var firstName;

var lastName;

var custName;

var state;

// Declare constants

var BR = "<br />";

var ES = "";

2 Answers

Relevance
  • ?
    Lv 7
    7 years ago

    BR, ES, and pkgCost are undefined. I see them being used but they don’t have any values set in the code posted.

    I get an unclosed string error from these two lines.

    var BR = "

    ";

    I assume that this isn't like this in your code, but the correct code is:

    var BR = "";

    pkgCost is still not defined. ^_^; I think you mean to write pkgPrice instead of pkgCost. The code breaks here so it never gets to writing the stuff to the screen due to the error.

    <!DOCTYPE html>

    <html>

    <head>

        <title>Page Title</title>

    </head>

    <body>

    <script>

    // Declare constants

    var BR = "";

    var ES = "";

    // Declare variables

    var numPkgs;

    var pkgPrice;

    var totPkgCost;

    var shipCost;

    var totOrderCost;

    var firstName;

    var lastName;

    var custName;

    var state;

    var output = "";

    // Ask for ordering information

    firstName = prompt("Enter your name", ES);

    lastName = prompt("Enter your last name", ES);

    numPkgs = prompt("Enter number of packages", ES);

    state = prompt("State to ship to", ES);

    // Concatentate first and last name

    custName = firstName + " " + lastName;

    // Compute per package price and shipping cost

    if (numPkgs >= 6) {

        pkgPrice = "2.00";

    }

    else {

        pkgPrice = "2.50";

    }

    if (state === "ID") {

        shipCost = "0.00";

    }

    else {

        shipCost = "4.00";

    }

    // Compute

    totPkgCost = pkgPrice * numPkgs;

    totOrderCost = totPkgCost + shipCost;

    // Collect output

    output += "Welcome to Bulk Is Best!" + BR;

    output += "Order Summary for " + custName + BR;

    output += "Number packages ordered: " + numPkgs + BR;

    output += "Price per package: " + pkgPrice + BR;

    output += "Number packages ordered: " + numPkgs + BR;

    output += "Price per package: " + pkgPrice + BR;

    output += "Subtotal: " + totPkgCost + BR;

    output += "Order will be shipped to: " + state + BR;

    output += "Shipping cost: " + shipCost + BR;

    output += "Order total: " + totOrderCost + BR;

    // Print to screen

    document.write(output);

    </script>

    </body>

    </html>

  • 7 years ago

    Fixed completely.

Still have questions? Get your answers by asking now.