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 0
s.
Remove leading zeros from a number in Javascript
We can use four methods for this conversion
- parseInt with radix
10
- Number Constructor
- Unary Plus Operator
- 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
Extract Part of Url With Regex
Blob Createobjecturl Download Not Working in Firefox (But Works When Debugging)
How to Parse Through Local Json File in React Js
How to Toggle Class to a Div Element in Reactjs
React Use State to Show Otherwise Hide
How to Format a Number to 2 Decimal Places, But Only If There Are Already Decimals
Calculate the Bounding Box'S X, Y, Height and Width of a Rotated Element Via JavaScript
Connection Refused! Is Selenium Server Started
How to Detect If a Browser Is Blocking a Popup
How to Pass Value into Href Link of a Bootstrap Modal
Pull Variable Value from JavaScript Source Using Beautifulsoup4 Python
Why the Value of Input Elements Are Not Changing
Is There Any Workaround (Or Hack) to Make the Audio Autoplay on Chrome, Firefox and Safari
Want to Hide Menu by Clicking Outside the Menu Element
Navigationduplicated Navigating to Current Location ("/Search") Is Not Allowed
Focus Next Input Once Reaching Maxlength Value
How to Check Whether Dynamically Attached Event Listener Exists or Not