JavaScript Ne(eeeeee)rds. Help gotten! Issue solved. All hail Crowmolly!

Illustration for article titled JavaScript Ne(eeeeee)rds. Help gotten! Issue solved. All hail Crowmolly!

I'm terribly sorry about going so far off-topic, but I am ripping my hair out right now and I've got nowhere else to go.

Advertisement

A friend of mine boasted she'd made a JavaScript cash register so I decided to try and make one of my own. I got it working with some very basic, repetitive code. I wanted to challenge myself and streamline it today, but I'm running into problems with it. I have declared global variables cashNumber and costNumber. Both of which are set to 0.

var cost; var costNumber = 0; var cash; var cashNumber = 0; var cashBack; var costText = ("Cost of merchandise"); var cashText = ("Cash Paid");

function costPrompt() { dataPrompt(cost, costText, costNumber, costPrompt); }

function cashPrompt() { dataPrompt(cash, cashText, cashNumber, cashPrompt); }

function dataPrompt(x, y, z, a) { x = prompt (y); z = parseFloat(x); console.log (z); console.log (a); if (isNaN(z)) { alert ("That's not a number, cumface. Try again."); a(); } }

Advertisement

costPrompt();

cashPrompt();

console.log ("cashNumber " + cashNumber); console.log ("costNumber " + costNumber);

Can any one explain to me why the changes made to cashNumber and costNumber inside dataPrompt does not change outside the function, even though the variable is global?

Advertisement

Share This Story

Get our newsletter