-1

I am having an issue implementing this calculator on my website which i created. I think i have used the wrong javascript to create the simple calculation which is the following math calculation: ((list price - rrp) / list price) * 100

PLEASE NOTE, i am aware of the values not being numbers, please replace them with any numbers. it still doesnt work.

This is to get the percentage value of the discount against the list and the RRP.

Please review the code before HTML:

 <script type="text/javascript">
   discountFinal(#OriginalPrice, #ListPrice);
 </script>
<div id="discountCode">
   <span id="spanish"></span>
   <span id="spanishtwo">%</span>
</div>

Javascript:

var discountFinal = function (firstly, secondly) {
                var totalfirst = secondly - firstly;
                var totalsecond = totalfirst / secondly;
                var totalthird = totalsecond * 100;
                if (document.getElementById("discountCode").innerHTML === null) {
                    document.getElementById("spanishtwo").innerHTML.replace("%", "")
                } else {
                    document.getElementById("spanish").innerHTML = Math.floor(totalthird / 5) * 5
                }
            };

I dont think i am calling the function within the html properly. Can someone assist with this please.

http://jsfiddle.net/xwzhY/

6
  • 1
    What did you add a perl-tag to your question? Commented Jul 22, 2013 at 19:07
  • Please review the question, there are perl tags within the call of the javascript. I added jquery to find out of using jquery might be a better option to implement this calculation. Commented Jul 22, 2013 at 19:10
  • So the HTML is issued by a Perl script? In that case, you will have to show us some Perl code. Commented Jul 22, 2013 at 19:12
  • gist.github.com/jagmitg/6056700 full code Commented Jul 22, 2013 at 19:13
  • What templating language is this? Commented Jul 22, 2013 at 19:23

3 Answers 3

2

I'm not sure the error you're getting, but it seems as if you're calling the discountFinal function before it's defined. When you move the call, it starts to work:

http://jsfiddle.net/bmonty/xwzhY/4/

Edit after comment from OP.

You just need to make sure your discountFinal function is defined at the top of your page, before any place it gets called.

This will work:

<script type="text/javascript">
  var discountFinal = function(a, b){};
</script>

<script type="text/javascript">
  var result = discountFinal(1, 2);
</script>

But this will throw an error:

<script type="text/javascript">
  var result = discountFinal(1, 2);
</script>

<script type="text/javascript">
  var discountFinal = function(a, b){};
</script>

To get some clarification, View Source on the HTML page from your browser to see what the resulting page looks like. That should point out where the order of operations is getting messed up.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for this, but the issue is that the reason i was separating the javascript with the call function is because the perl variables will go inside the calculation. I dont want the javascript to repeat, i just want the document to repeat with the discount.
JSG, I updated my post to clarify. Does that make more sense?
Thank you very much! didnt know i had to declare variable when calling function.
1

It works fine if you call your function after it exists: http://jsfiddle.net/xwzhY/2/

Just make sure that the function is declared earlier in the code than you use it. Or declare it using a function statement rather than a function expression assigned to a variable:

function discountFinal(firstly, secondly){

...

Comments

0

Trying put "" around your perl variable, you need to pass the value

1 Comment

thanks for the reply. No it doesnt work. When in the HTML, the perl variables are automatically translated to numbers but there is an issue with the js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.