-1

This is for an assignment and I cannot get it to output anything when I click the Submit button. It needs to be in XHTML 1.0 Strict per the instructor. Thank you!

<!DOCTYPE html
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title> "Calculate Shipping" </title>
<script type="text/javascript">
  // <![CDATA[
function calculateShipping() }
    var price = parseFloat(document.getElementById('price').value);
       This will add $1.50 to any purchases that are less than or equal
to $25.00.
    if (price <= 25){
        price = (price) + 1.5;
    } else {
        //return price * 10 / 100
        var percentToAdd=(price) * .1;
        price=(price)+(percentToAdd);
    }

    document.getElementById('result').innerHTML='Total Order Cost:
'+price;

// ]]>
</script>
</head>

<body>
<h1>Enter Purchase Price</h1>

<form action="#">
<div id="result">
<input type="text" name="price" id="price" />
<input type="button" value="Submit" onclick="calculateShipping(); return
false;" />
</div>
</form>
</body>
</html>
  • Javascript doesn't 'do' multiple lines unless you escape each line. On top of that, you never use the correct bracket to open your function, nor do you close it. Your browser has a javascript console for a reason. Hit F12 in pretty much any modern browser. – Daedalus Jan 16 '14 at 03:13
  • I'm not sure I understand what you are saying? I did have this working when I had it in HTML. – user3199950 Jan 16 '14 at 03:16
  • I refuse to believe you; your code syntax is incorrect. – Daedalus Jan 16 '14 at 03:17
  • Oh it definitely was more simplified than this. I just started making changes based on what the validation tool was telling me to do. Now it passes validation but doesn't work. I did run it through the debugger and it says that calculateShipping is undefined. Wouldn't that have been defined under the function command? – user3199950 Jan 16 '14 at 03:24
  • 1
    ... but the JS certainly did not work – Ed Heal Jan 16 '14 at 03:24
  • @user3199950 - The validation tool just validates the markup - not the JS – Ed Heal Jan 16 '14 at 03:25

2 Answers2

1

What is this doing in the middle of the JS:

 This will add $1.50 to any purchases that are less than or equal to $25.00.

Remove in or comment it

And this

document.getElementById('result').innerHTML='Total Order Cost:
'+price;

Should be on the one line

Also need the correct opening brace at the start of the function and you also need to closing brace at the end

Use the browsers debugger

BTW - You do not have to be so liberal with the parenthesis

EDIT

Here you go

<script type="text/javascript">
function calculateShipping() {
    var price = parseFloat(document.getElementById('price').value);
    document.getElementById('result').innerHTML='Total Order Cost: '+ (price <= 25 ? price + 1.5 : price * 0.1);
}
</script>
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • It still tells me that the calculateShipping is undefined, is the code that messed up or am I missing something simple here? – user3199950 Jan 16 '14 at 03:34
  • I just noticed your edit with the code...that's all I need instead of that mess that I created? – user3199950 Jan 16 '14 at 03:43
  • @user3199950 .. Where did that second opening bracket come from? – Daedalus Jan 16 '14 at 03:45
  • @Daedalus - Typo on my part - fixed – Ed Heal Jan 16 '14 at 03:50
  • @EdHeal No offense, but are you going to explain your answer? This user is a beginner.. just handing them code won't work. I'm currently writing my own answer to address this, just felt the need to alert you of my feelings. – Daedalus Jan 16 '14 at 03:51
  • @Daedalus - I have pointed out the mistakes. I have produced a answer. I guess the poster can figure out why it works. For heavens sake it is 4am here. – Ed Heal Jan 16 '14 at 03:54
  • @EdHeal Go to bed then :/; I know that feeling. Stayed up till 3am several nights in a row solving a bug or more. – Daedalus Jan 16 '14 at 03:55
  • I really appreciate your help on this @Daedalus and Ed. I really thought I had it earlier...and earlier...and one other time too. :) – user3199950 Jan 16 '14 at 03:57
0

Since you asked for an explanation, instead of just the answer, here's your explanation:

As I said in my comment, there are multiple problems with your code, I will address each in a comment adjacent or below the problem, indicating what is wrong, and why.

function calculateShipping() } // This is not an opening bracket.
// All functions(by my understanding of js) need an opening curly bracket({)
// in order to begin their declaration.  You also need a closing curly bracket
// (}) to end it.
    var price = parseFloat(document.getElementById('price').value);
       This will add $1.50 to any purchases that are less than or equal
// On top of the fact that the 'this' keyword is reserved, the browser is
// going to treat this as multiple undefined variables, objects, etc,
// and unless this line is commented out, as this explanation is, your code
// will not work.
to $25.00.
// ^ same thing here; 'to' is undefined, as is $25, which is likely going to be
// treated as an variable holding an object.  This is because the dot(.)
// is used to access either properties(variables) or methods(functions) of an
// object.
    if (price <= 25){
        price = (price) + 1.5;
    } else {
        //return price * 10 / 100
        var percentToAdd=(price) * .1;
        price=(price)+(percentToAdd);
    }

    document.getElementById('result').innerHTML='Total Order Cost:
'+price;
    // Javascript does not do multi-lines like php; you either have to escape
    // them or, you have to quote them.  I will give examples of this below.

// And here..  here you do nothing.  As noted above, you need a closing
// curly bracket to end your function declaration.

And now for the proper way to code this function;

function calculateShipping() { // opening curly brackte
    var price = parseFloat(document.getElementById('price').value);
    /* This will add $1.50 to any purchases that are less than or equal
       to $25.00.
    */
    // ^ a multi-line comment.
    if (price <= 25){
        price = (price) + 1.5;
        // This is really fine, except you don't really need () around 'price'.
    } else {
        //return price * 10 / 100
        var percentToAdd=(price) * .1;
        price=(price)+(percentToAdd);
        // Same thing as above regarding parenthesis.
    }

    document.getElementById('result').innerHTML='Total Order Cost: \
    '+price;                                    // Escape the line ^
} // Finally, end the function declaration.

As noted though, there are multiple ways to account for extra lines; here is one:

var some_var = 'this is a string ' +
'this is another string';

Given the above, the contents of some_var will be:

this is a string this is another string

If you want your code to be on separate lines, that's another story. You need html for that.. specifically; break tags:

var some_var = 'this is a string<br />this is a string on another line';

And last but not least, your if/else can be reduced to a ternary operator; a ternary operator is a (usually) one line method of if/else clauses. Here is an example:

var some_var = (1 < 2) ? 'true' : 'false';
        //     boolean   is true   is false

In the above, 1 is less than 2, and thus, some_var will be set to the string 'true'. Otherwise, it will be set to 'false'. Thus, your above if/else can be simplified to:

price = (price <= 25)?(price+1.5):(price+(price* .1));
Daedalus
  • 7,586
  • 5
  • 36
  • 61