-4

I have searched and I can see there is other questions like this, but I don't understand how to implement it.

This is my first function:

function pricecalc() {
  var a = document.getElementById("field_0");
  var quantity = a.value.substring(0, 1);
  var b = document.getElementById("field_1");
  var type = b.value;

  if (quantity == '2') {
    var rate = '120';
  } else if (quantity == '3') {
    var rate = '110';
  } else {
    var rate = '100';
  }

  var price = rate * quantity;

  if (type == 'Credit card') {
    var price = price * 1.034;
  }

  var price_each = (price / quantity);

  document.getElementById("cost").innerHTML = price;
  document.getElementById("costeach").innerHTML = price_each;
}

Then under it I have this:

function PHPFMG( formID ){
  var redirect = 'https://www.paypal.me/' + price;
}

But I can't access the price variable in the second function. I really don't want to bodge this by setting some text on the page with the first function and then pulling it with the second function.

I looked at creating a global variable but if I set that in my top function and then pulled it in my second it only seemed to have it's original value.

connexo
  • 53,704
  • 14
  • 91
  • 128
Jimmy
  • 12,087
  • 28
  • 102
  • 192
  • 1
    When you make it a global, make sure you aren't redefining a local variable called price still. – Kevin DiTraglia Jan 31 '18 at 22:05
  • Don't you have access to document.getElementById("cost") element if yes then you can't you get the value that you are looking for – Maddy Jan 31 '18 at 22:05
  • 3
    Is this like your 20th question with this code this hour? – epascarello Jan 31 '18 at 22:07
  • At the end of `function pricecalc()` add `return price;`. In your 2nd function replace `price` by `pricecalc()`. – connexo Jan 31 '18 at 22:07
  • [How](https://stackoverflow.com/q/48530742/1541563) [many](https://stackoverflow.com/q/48549601/1541563) [questions](https://stackoverflow.com/q/48550391/1541563) are going to be about [this](https://stackoverflow.com/q/48551009/1541563) [assignment](https://stackoverflow.com/q/48551681/1541563)? – Patrick Roberts Jan 31 '18 at 22:10
  • 1
    There should be a "Closed because asked before numerous times with same code" flag – StudioTime Jan 31 '18 at 22:10
  • 1
    I'm voting to close this question as off-topic because it's been asked in different ways more than once – StudioTime Jan 31 '18 at 22:12
  • @PatrickRoberts It's for a wedding not an assignment – Jimmy Jan 31 '18 at 22:22

5 Answers5

1

You have price declared inside the function, not globally.

Its inside the priceCalc function and therefore local to there, cant be referenced outside of it. So declare the price val globally outside the function with

var price; //outside of function
function priceCalc(){}

Also why do you declare price twice?

 var price = rate * quantity; //declared here
 if (type == 'Credit card') {
 var price = price * 1.034; //and here
 }
JakeTheSnake
  • 372
  • 1
  • 11
1

You need to initialise the price varible out of any function

var price = 0;
function pricecalc() {
  var a = document.getElementById("field_0");
  var quantity = a.value.substring(0, 1);
  var b = document.getElementById("field_1");
  var type = b.value;

  if (quantity == '2') {
    var rate = '120';
  } else if (quantity == '3') {
    var rate = '110';
  } else {
    var rate = '100';
  }

  price = rate * quantity;

  if (type == 'Credit card') {
    price = price * 1.034;
  }

  var price_each = (price / quantity);

  document.getElementById("cost").innerHTML = price;
  document.getElementById("costeach").innerHTML = price_each;
}
Patrick Schocke
  • 1,493
  • 2
  • 13
  • 25
  • Hello. This works except in pricecalc function the value of price is correct but in the second function price still has value of 0; – Jimmy Jan 31 '18 at 22:13
  • Then you must have defined a local variable named `price` in your second function as well. Or you still have `var price` inside your first function too. – connexo Jan 31 '18 at 22:14
  • i've made a mistake: `if (type == 'Credit card') { var price = price * 1.034; } ` only `price = price * 1.034;` is correct. You don't need to initialise price a second time – Patrick Schocke Jan 31 '18 at 22:17
  • The price value in the first function doesn't move across to the second function though. I.e if my price is displayed as 600 in the first function my redirect won't go to paypal.me/600 – Jimmy Jan 31 '18 at 22:49
  • If you only initialise the variable once and outside the function it should work. Are you sure that you don't initialise price in the second function? – Patrick Schocke Jan 31 '18 at 22:54
  • Yes. In your example I just end up with the value 0 if I try and access price in my second function – Jimmy Jan 31 '18 at 23:09
0

You can solve the problem by having your first function return price; and replacing price in your 2nd function by a call to your first function:

function pricecalc() {
  var quantity = Number(field_0.value.charAt(0));
  var type = field_1.value;
  var rate, price, price_each;

  if (quantity === 2) {
    rate = 120;
  } else if (quantity === 3) {
    rate = 110;
  } else {
    rate = 100;
  }

  price = rate * quantity;

  if (type === 'Credit card') {
    price *= 1.034;
  }

  price_each = (price / quantity);

  cost.innerHTML = price;
  costeach.innerHTML = price_each;

  return price;
}

function PHPFMG( formID ){
  var redirect = 'https://www.paypal.me/' + pricecalc();
}

This way you don't have to introduce a global variable price.

connexo
  • 53,704
  • 14
  • 91
  • 128
  • Hello. This is a great suggestion but it always seems to return the value 240, even when I update the quantity – Jimmy Jan 31 '18 at 22:20
  • It still doesn't seem to work for me. It always links me to www.paypal.me/240 no matter what options I pick. The text updates ok, just the form redirect is always to 240 – Jimmy Jan 31 '18 at 22:37
  • `function PHPFMG( formID ){ var redirect = 'https://www.paypal.me/' + pricecalc(); }` you do realize this function does nothing more than declaring a local variable? What are you expecting this function to do? – connexo Jan 31 '18 at 22:39
  • I wanted it to grab the price variable from my other function. I.e if the text says 600, then redirect to paypal.me/600. No matter what option I pick, and what number is displayed under the form, it always goes to paypal.me/240 – Jimmy Jan 31 '18 at 22:40
  • Declaring a variable `redirect` will not have **any** effect on that. That's just as effective as declaring `var king_of_the_world="Jimmy"` - don't expect to be crowned. – connexo Jan 31 '18 at 22:41
0

var price = 0;
function pricecalc() {
  var a = document.getElementById("field_0");
  var quantity = a.value.substring(0, 1);
  var b = document.getElementById("field_1");
  var type = b.value;

  if (quantity == '2') {
    var rate = '120';
  } else if (quantity == '3') {
    var rate = '110';
  } else {
    var rate = '100';
  }

  price = rate * quantity;

  if (type == 'Credit card') {
    var price = price * 1.034;
  }

  var price_each = (price / quantity);

  document.getElementById("cost").innerHTML = price;
  document.getElementById("costeach").innerHTML = price_each;
}

function PHPFMG( formID ){
  var redirect = 'https://www.paypal.me/' + pricecalc();
}
Rakib Hasan
  • 317
  • 1
  • 5
0

You can't access the variable in the second function because of scope, you could always create a function inside the body of the function that would have access to the variable or use an anonymous function inside the body of that function that would have access or pass the second function as a parameter and execute it inside the function that contains the price variable declaration which is called a callback. The other option is to declare the variable outside the body of the function, but keep in mind that is global scope and can create problems and is considered bad practice, especially when working with libraries. Here is a link about scope in javaScript variables, MDN is a good reference add it to your bookmarks https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var and here is an article about callbacks http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/ to pass the function as a callback

`function priceCalc( formID, callback) {
//your code goes here
callback( formId); // the callback function is now executed and has access 
// to the variables inside priceCalc
} // end function
// execute priceCalc
priceCalc( formID, PHPFMG );`