Java Script Help

Hey guys, I guess my question is rather simple for someone who knows Java Script.

We are going to use a WordPress calculator to write invoices to parents for books and we bought a plugin for it. What it can’t do without Java Script is to calculate percentages.

What I need is, if a box for a rebate is checked, that the total is reduced by 20%.


The variable for the total is [zgfm_fvar opt=“form_total_amount”] in this Addon.

I tried to do it myself, but 2 hours were not enough to understand how it works.


This does not work, but maybe it shows what I want to achieve. :sweat_smile:

Can anyone help me, please? :sweat_smile:

From my friend named GPT:

The image you’ve uploaded appears to contain a snippet of JavaScript code, but without a broader context or error messages, it’s hard to pinpoint the exact reason why it’s not working as expected. However, I can offer a few insights based on common issues:

  1. Variable Initialization: The variable total is declared but not initialized to a value. This could lead to NaN (Not a Number) results when you try to perform arithmetic operations on it.

  2. Conditional Check: The if statement is checking fld_uij9xsnvjfd_isChecked. If this is meant to be a function, it should have () at the end to call it. If it’s meant to be a boolean variable, ensure that it is defined and set properly before this block of code.

  3. Element Selection: The syntax [zgfm_fvar opt="form_total_amount"] is not a standard JavaScript or jQuery selector. It looks like a custom function or library is being used, which should be checked for correctness.

  4. Assignment and Multiplication: The line within the if statement is using an assignment within the array brackets and then immediately multiplying by 0.9. The assignment should be separated from the multiplication for clarity, and to ensure it behaves as expected.

  5. Missing Information: There is no code shown for how total is updated. It is returned at the end, but it’s never modified from its initial (undefined) state within the visible code.

  6. Code Convention: The code uses unconventional naming and syntax, which could be part of a specific framework or library. If so, ensure that the framework or library is correctly included and that the syntax matches its requirements.

To resolve the issue, consider the following changes:

  • Initialize total with a starting value, e.g., var total = 0;.
  • Confirm that fld_uij9xsnvjfd_isChecked is defined and returning the expected value.
  • If using a framework or library for the opt attribute, ensure it is included and used correctly.
  • Separate the assignment from the multiplication operation.

Here is a revised version of the code with some assumptions:

var total = 0; // Initialize total to 0 or some other default value.

if (fld_uij9xsnvjfd_isChecked()) { // Assuming it's a function that returns a boolean.
  var amount = zgfm_fvar("form_total_amount"); // Assuming zgfm_fvar is a function that retrieves a value.
  total = amount * 0.9; // Multiply the amount by 0.9 and assign it to total.
}

return total;

Please replace the zgfm_fvar and fld_uij9xsnvjfd_isChecked with the correct function calls or variable references as per the framework or library you’re using.

1 Like

If you trust a client-side executed javascript to calculate the order price you gonna have a baaaad time…
image

do you think he should repost every character that is typed in order to get the total?

I’m assuming Philipp is just generating a non binding estimate of an invoice, not actually going from there to a credit card processing.

Fabien is right that anyone can make that form say whatever price they want if it is calculated client side.

This advice isn’t too bad. I agree with the robot that we don’t have quite enough context.

Can you tell that the ischecked variable is correct? You can add console.log("it is checked") in the if and check the console (from the developer menu).

I would usually structure it more like this:

var multiplier = 1.0
if (checked)
{
    multiplier = 0.9
}
return form * multiplier

Then you can add some debug console prints like this:

var multiplier = 1.0
if (checked)
{
    multiplier = 0.9
    console.log("isChecked")
}
console.log(multiplier)
cobsole.log(form)
return form * multiplier

I use these to poke at stuff I don’t understand. Then I do ctrl+shift+I and the logs are in the console tab. I usually remove these once I am satisfied. A more in depth software engineering task would involve more instrumentation. But just debugging a single function while learning can really be helped with some logs.

1 Like