Prevent JavaScript String That Contains a Number from Dropping Zeros

Stop JavaScript from truncating leading 0

Make it a string then:

echo("var zipCode = '" . $zipCode . "';");

Notice the two ' characters before and after the concatenating of $zipCode.

I'm sure $zipCode is a string in your PHP, but when you echo it, it doesn't include the quotes. That means your resulting Javascript would look like:

var zipCode = 01234;

And there's no reason to store it as a number (incorrect number, in this case, since it will think it's an octal number).

So by adding the ' characters, the resulting Javascript becomes:

var zipCode = '01234';

And should be just fine with manipulating, since you should really only be doing string manipulations on a zip code anyways.

If, for whatever reason, you decide that you need the Number form of the zip code, you can use this in Javascript:

var zipCodeNum = parseInt(zipCode, 10);

The important part is the , 10, as this will force the conversion to be in base 10, ignoring any leading 0s.

Remove leading zeros from a number in Javascript

We can use four methods for this conversion

  1. parseInt with radix 10
  2. Number Constructor
  3. Unary Plus Operator
  4. Using mathematical functions (subtraction)

const numString = "065";
//parseInt with radix=10let number = parseInt(numString, 10);console.log(number);
// Number constructornumber = Number(numString);console.log(number);
// unary plus operatornumber = +numString;console.log(number);
// conversion using mathematical function (subtraction)number = numString - 0;console.log(number);

How to stop decreasing number if it have reach zero?

In JavaScript:

// In the onClick event handler:
coins = (coins <= 50) ? 0 : coins - 50;
return coins;

In words:

If coins are less than or equal to fifty make coins zero; otherwise, reduce coins by fifty.

I see you're using inline JavaScript:

<button onclick='(golds <= 50) ? golds = 0 : gold -= 50;'>Buy</button>

While it's possible to do that, it's best to keep logic like this out of the HTML. It's a better idea to set an eventListener on that element that responds to click events.

// used to be:
// <button onclick='golds -= 50'>Buy</button>
<button id="sellIronSword">Buy</button>

// Inside a <script>
var ironSwordButton = document.getElementById('sellIronSword');
ironSwordButton.addEventListener('click', function() {
coins = (coins <= 50) ? 0 : coins - 50;
return coins;
}

Try it out:

var coins = 150;var ironSwordButton = document.getElementById('sellIronSword');
ironSwordButton.addEventListener('click', function() { coins = (coins <= 50) ? 0 : coins - 50; document.getElementById('coins').innerHTML = coins;});
<h3>Coins: <span id="coins">150</span></h3><hr /><label>  <span>Buy Iron Sword</span>  <button id="sellIronSword">Buy</button></label>

Remove/ truncate leading zeros by javascript/jquery

You can use a regular expression that matches zeroes at the beginning of the string:

s = s.replace(/^0+/, '');

Javascript prevent decrement past 0

You simply check if the counter is still greater than zero and only if so you actually decrement your counter

decrement(){
if(this.counter > 0){
this.counter -= 1
}
}


Related Topics



Leave a reply



Submit