Javascript: Auto Converting numbers to prefixes - javascript

I'm creating an idle game(Cookie Clicker etc.), it seems that when my players reach a high amount of clicks, the game starts to slow down.
High numbers doesn't fit well in the game either since it takes up too much space. So is there a script that converts every number to a prefix?
Example:
10 = 10
10000000 becomes 1 million.
1,000,000,000 becomes 1 billion
1,000,000,000,000 becomes 1 trillion
1.4 quadrillion would be 1400000000000000
It's quite similar to this.
Cookie Clicker and swarm simulator has the feature that I'm looking for.
Edit: Thanks, Drew Quick!
For those who's interested:
var count = 1;
function main() {
count += 1000;
var str = count.toString();
var tmpCount = '';
if (count < 1000000) {
tmpCount = "";
} else if (count > 1000000 && count < 1000000000) {
str = "Million";
tmpCount = (count / 1000000).toFixed(2);
} else if (count > 1000000000 && count < 1000000000000) {
str = "Billion";
tmpCount = (count / 1000000000).toFixed(2);
} else if (count > 1000000000000 && count < 1000000000000000) {
str = "Trillion";
tmpCount = (count / 1000000000000).toFixed(2);
} else if (count > 1000000000000000 && count < 1000000000000000000) {
str = "Quadrillion";
tmpCount = (count / 1000000000000000).toFixed(2);
} else if (count > 1000000000000000000 && count < 1000000000000000000000) {
str = "Quintillion";
tmpCount = (count / 1000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000 && count < 1000000000000000000000000) {
str = "Sextillion";
tmpCount = (count / 1000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000 && count < 1000000000000000000000000000) {
str = "Septillion";
tmpCount = (count / 1000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000 && count < 1000000000000000000000000000000) {
str = "Octillion";
tmpCount = (count / 1000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000 && count < 1000000000000000000000000000000000) {
str = "Nonillion";
tmpCount = (count / 1000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000 && count < 1000000000000000000000000000000000000) {
str = "Decillion";
tmpCount = (count / 1000000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000000 && count < 1000000000000000000000000000000000000000) {
str = "Undecillion";
tmpCount = (count / 1000000000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000000000 && count < 1000000000000000000000000000000000000000000) {
str = "Duodecillion";
tmpCount = (count / 1000000000000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000000000000 && count < 1000000000000000000000000000000000000000000000) {
str = "Tredecillion";
tmpCount = (count / 1000000000000000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000000000000000 && count < 1000000000000000000000000000000000000000000000000) {
str = "Quattuordecillion";
tmpCount = (count / 1000000000000000000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000000000000000000 && count < 1000000000000000000000000000000000000000000000000000) {
str = "Quindecillion";
tmpCount = (count / 1000000000000000000000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000000000000000000000 && count < 1000000000000000000000000000000000000000000000000000000) {
str = "Sexdecillion";
tmpCount = (count / 1000000000000000000000000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000000000000000000000000 && count < 1000000000000000000000000000000000000000000000000000000000) {
str = "Septendecillion";
tmpCount = (count / 1000000000000000000000000000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000000000000000000000000000 && count < 1000000000000000000000000000000000000000000000000000000000000) {
str = "Octodecillion";
tmpCount = (count / 1000000000000000000000000000000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000000000000000000000000000000 && count < 1000000000000000000000000000000000000000000000000000000000000000) {
str = "Novemdecillion";
tmpCount = (count / 1000000000000000000000000000000000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000000000000000000000000000000000 && count < 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) {
str = "Vigintillion";
tmpCount = (count / 1000000000000000000000000000000000000000000000000000000000000000).toFixed(2);
} else if (count > 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 && count) {
str = "Googol";
tmpCount = (count / 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000).toFixed(2);
}
document.getElementById("count").innerText = tmpCount + ' ' + str;
setTimeout(function() {
main();
}, 1);
}
main();
<span id="count">test</span>
Hope it helps!!

How can I convert numbers into scientific notation?
var clicks = 100000000000000000000000;
alert(clicks.toExponential());
Edit: What about something simple and straight forward like so? This is rough by the way. Just to get the idea across.
var count = 1;
function main() {
count += 1000;
var str = count.toString();
var tmpCount;
if (count < 1000000) {
tmpCount = "";
} else if (count > 1000000 && count < 1000000000) {
str = "million";
tmpCount = (count / 1000000).toFixed(2);
} else if (count > 1000000000 && count < 1000000000000) {
str = "billion";
tmpCount = (count / 1000000000).toFixed(2);
}
document.getElementById("count").innerText = tmpCount + ' ' + str;
setTimeout(function() {
main();
}, 1);
}
main();
<span id="count">test</span>

Related

How to add the operators * and / in this calculation algorithm

const calculate = (s) => {
let code;
let index;
let startIndex;
let stackSign = [];
let result = [0];
let numberSign = 1;
for (var i = 0; i < s.length; i++) {
if (s[i] === ' ') {
continue;
} else if (s[i] === '(') {
stackSign.push(numberSign);
result.push(0);
numberSign = 1;
} else if (s[i] === ')') {
result[result.length - 2] += result.pop() * stackSign.pop();
} else if (s[i] === '+') {
numberSign = 1;
} else if (s[i] === '-') {
numberSign = -1;
} else if (s[i] === '/') {
numberSign = result / 1;
} else if (s[i] === '*') {
numberSign = result * 1;
} else {
startIndex = i;
while (
(index = i + 1) < s.length &&
(code = s[index].charCodeAt()) > 47 &&
code < 58
) {
i++;
}
result[result.length - 1] +=
+s.substring(startIndex, i + 1) * numberSign;
}
}
return result.pop();
};
Hello guys i made this algorithm to create a calculator without javascript "eval" but im struggling to add the * and / operators, can you guys help me with that? thanks
How i'm using it:
calculate('50 + 50');

Javascript formatting of 10 or 11 digit phone numbers and recognizing a 1800 phone number

I am trying to get javascript to format phone numbers based on a users input of 10 or 11 digits. The 11 digits are for phone numbers that start with a 1 at the beginning like a 1-800 number. I need the final output to be either 000-000-0000 or 1-000-000-0000. The sample javascript code that I was given to start out with, works with the 10 digit phone number but I need the javascript to also recognize if there is a 1800 number and append accordingly.
The following is my initial working javascript and below that is code I found online that addresses the 10 and 11 digital formatting however I don’t know how to mesh the two together.
Thank you in advance for any help given.
~~~~~~~~~~~~~~~~~
<script type="text/javascript">
var phoneNumberVars = [ "UserProfilePhone", "UserProfilePhone1", "UserProfilePhone2", "UserProfilePhone3", ];
InitialFormatTelephone();
function InitialFormatTelephone()
{
for (var i = 0; i < phoneNumberVars.length; i++)
{
FormatTelephone(phoneNumberVars[i]);
}
}
function StorefrontEvaluateFieldsHook(field)
{
for (var i = 0; i < phoneNumberVars.length; i++)
{
if (field.id == "FIELD_" + FieldIDs[phoneNumberVars[i]])
{
FormatTelephone(phoneNumberVars[i]);
}
}
}
function FormatTelephone(varName)
{
var num = document.getElementById("FIELD_" + FieldIDs[varName]).value;
var charArray = num.split("");
var digitCounter = 0;
var formattedNum;
if (charArray.length > 0)
formattedNum = “-“;
else
formattedNum = "";
var i;
for (i = 0;i < charArray.length; i++)
{
if (isDigit(charArray[i]))
{
formattedNum = formattedNum + charArray[i];
digitCounter++;
if (digitCounter == 3)
{
formattedNum = formattedNum + “-“;
}
if (digitCounter == 6)
{
formattedNum = formattedNum + "-";
}
}
}
if (digitCounter != 0 && digitCounter != 10)
{
alert ("Enter a valid phone number!");
}
// now that we have a formatted version of the user's phone number, replace the field with this new value
document.getElementById("FIELD_" + FieldIDs[varName]).value = formattedNum;
// force an update of the preview
PFSF_AjaxUpdateForm();
}
function isDigit(aChar)
{
myCharCode = aChar.charCodeAt(0);
if((myCharCode > 47) && (myCharCode < 58))
{
return true;
}
return false;
}
</script>
<script type="text/javascript">
var phoneNumberVars = [ "UserProfilePhone", "UserProfilePhone1", "UserProfilePhone2", "UserProfilePhone3", ];
InitialFormatTelephone();
function InitialFormatTelephone()
{
for (var i = 0; i < phoneNumberVars.length; i++)
{
FormatTelephone(phoneNumberVars[i]);
}
}
function StorefrontEvaluateFieldsHook(field)
{
for (var i = 0; i < phoneNumberVars.length; i++)
{
if (field.id == "FIELD_" + FieldIDs[phoneNumberVars[i]])
{
FormatTelephone(phoneNumberVars[i]);
}
}
}
function FormatTelephone(varName)
{
var num = document.getElementById("FIELD_" + FieldIDs[varName]).value;
var cleanednum = num.replace( /[^0-9]/g, "");
var charArray = cleanednum.split("");
var digitCounter = 0;
var formattedNum = "";
var digitPos1 = 0;
var digitPos3 = 3;
var digitPos6 = 6;
if (charArray.length ===11)
{
digitPos1++;
digitPos3++;
digitPos6++;
}
if (charArray.length > 0)
formattedNum = "";
else
formattedNum = "";
var i;
for (i = 0;i < charArray.length; i++)
{
if (isDigit(charArray[i]))
{
formattedNum = formattedNum + charArray[i];
digitCounter++;
if (digitCounter === digitPos1)
{
formattedNum = formattedNum + "-";
}
if (digitCounter == digitPos3)
{
formattedNum = formattedNum + "-";
}
if (digitCounter == digitPos6)
{
formattedNum = formattedNum + "-";
}
}
}
if ((charArray.length ==10 || charArray.length == 11 || charArray.length == 0) === false)
{
alert ("Enter a valid phone number!");
}
// now that we have a formatted version of the user's phone number, replace the field with this new value
document.getElementById("FIELD_" + FieldIDs[varName]).value = formattedNum;
// force an update of the preview
PFSF_AjaxUpdateForm();
}
function isDigit(aChar)
{
myCharCode = aChar.charCodeAt(0);
if((myCharCode > 47) && (myCharCode < 58))
{
return true;
}
return false;
}
</script>

Betfair like Odds increment and decrement

Please help me it is very irritating. Don't know why my logic is failed every time.
I am trying to make Betfair like odds increment in my web project. Betfair have it's own price group which can be found here
LINK: https://api.developer.betfair.com/services/webapps/docs/display/1smk3cen4v3lu3yomq5qye0ni/Betfair+Price+Increments
Here is explanation:
if odds is 1.01 and some body want to increase that odds via html5 number spinner the increment will be 0.01 and if odds is 2 the increment will be 0.02. whole increment list is available in that link.
working example can be found in betfair's betslip.
here is my Javascript:
function getIncremantal(fval) {
var val = parseFloat(fval);
var step;
if (val <= 1.99) {
step = 0.01;
} else if (val > 2 && val < 3) {
step = 0.02;
} else if (val > 3 && val < 4) {
step = 0.05;
} else if (val > 4 && val < 6) {
step = 0.1;
} else if (val > 6 && val < 10) {
step = 0.2;
} else if (val > 10 && val < 19.5) {
step = 0.5;
} else if (val >= 20 && val < 30) {
step = 1;
} else if (val >= 30 && val < 50) {
step = 2;
} else if (val >= 50 && val < 100) {
step = 5;
} else if (val >= 100 && val < 1000) {
step = 10;
} else if (val > 1000) {
step = null;
}
return step;
}
Update: jsFiddle
http://jsfiddle.net/71fs0a67/1/
I tried the following which is not using number stepping, but if you use the buttons it does work. It is an alternate solution, sorry if its not what you are looking for.
HTML:
<input type="number" min="1.01" max="1000" id="num"/>
<button class="increment">+</button>
<button class="decrement">-</button>
Javascript:
$('.increment').on('click', function() {
var elem = $('#num');
var value = parseFloat(elem.val());
var result = +(value + getIncremantal(value)).toFixed(2);
elem.val(result);
});
$('.decrement').on('click', function() {
var elem = $('#num');
var value = parseFloat(elem.val());
var result = +(value - getDecremantal(value)).toFixed(2);
elem.val(result);
});
function getIncremantal(val) {
var step;
if (val < 2) {
step = 0.01;
} else if (val >= 2 && val < 3) {
step = 0.02;
} else if (val >= 3 && val < 4) {
step = 0.05;
} else if (val >= 4 && val < 6) {
step = 0.1;
} else if (val >= 6 && val < 10) {
step = 0.2;
} else if (val >= 10 && val < 20) {
step = 0.5;
} else if (val >= 20 && val < 30) {
step = 1;
} else if (val >= 30 && val < 50) {
step = 2;
} else if (val >= 50 && val < 100) {
step = 5;
} else if (val >= 100 && val < 1000) {
step = 10;
} else if (val > 1000) {
step = null;
}
return step;
}
function getDecremantal(val) {
var step;
if (val <= 2) {
step = 0.01;
} else if (val > 2 && val <= 3) {
step = 0.02;
} else if (val > 3 && val <= 4) {
step = 0.05;
} else if (val > 4 && val <= 6) {
step = 0.1;
} else if (val > 6 && val <= 10) {
step = 0.2;
} else if (val > 10 && val <= 20) {
step = 0.5;
} else if (val > 20 && val <= 30) {
step = 1;
} else if (val > 30 && val <= 50) {
step = 2;
} else if (val > 50 && val <= 100) {
step = 5;
} else if (val > 100 && val <= 1000) {
step = 10;
} else if (val > 1000) {
step = null;
}
return step;
}
http://jsfiddle.net/71fs0a67/7/
With jquery ui spinner, you can do something like this:
$( "#spinner" ).spinner({
min: 1.01,
max: 1000,
step: 0.01,
spin: function( event, ui ) {
event.preventDefault();
event.stopPropagation();
var value = this.value || ui.value;
value = parseFloat(value);
var step;
if ($(event.currentTarget).hasClass('ui-spinner-up')) {
step = getIncremantal(value);
value = +(value + step).toFixed(2);
$( "#spinner" ).spinner('value', value);
} else {
step = getDecremantal(value);
value = +(value - step).toFixed(2);
$( "#spinner" ).spinner('value', value);
}
}
});
http://jsfiddle.net/71fs0a67/9/
Your code will return undefined for whole numbers.
Change all instances of val > number to val >= number
Try this:
function getIncremantal(fval) {
var val = parseFloat(fval);
var step;
if (val < 2) {
step = 0.01;
} else if (val >= 2 && val < 3) {
step = 0.02;
} else if (val >= 3 && val < 4) {
step = 0.05;
} else if (val >= 4 && val < 6) {
step = 0.1;
} else if (val >= 6 && val < 10) {
step = 0.2;
} else if (val >= 10 && val < 20) {
step = 0.5;
} else if (val >= 20 && val < 30) {
step = 1;
} else if (val >= 30 && val < 50) {
step = 2;
} else if (val >= 50 && val < 100) {
step = 5;
} else if (val >= 100 && val < 1000) {
step = 10;
} else if (val > 1000) {
step = null;
}
return step;
}
function getDecremantal(fval) {
var val = parseFloat(fval);
var step;
if (val <= 2) {
step = 0.01;
} else if (val > 2 && val <= 3) {
step = 0.02;
} else if (val > 3 && val <= 4) {
step = 0.05;
} else if (val > 4 && val <= 6) {
step = 0.1;
} else if (val > 6 && val <= 10) {
step = 0.2;
} else if (val > 10 && val <= 20) {
step = 0.5;
} else if (val > 20 && val <= 30) {
step = 1;
} else if (val > 30 && val <= 50) {
step = 2;
} else if (val > 50 && val <= 100) {
step = 5;
} else if (val > 100 && val <= 1000) {
step = 10;
} else if (val > 1000) {
step = null;
}
return step;
}

Check password strength against an API value

I get my password spec from an API which then I split the object into the needed fields and check that I have the required number of lower, upper, special and length of my password.
function isStrong(passwordChecker) {
if (!passwordChecker) {
return false;
}
debugger;
var securityOption = JSON.parse(localStorage.getItem("Security"));
var MinLength = securityOption.PasswordMinRequiredLength;
var SpecialChars = securityOption.PasswordMinRequiredNonalphanumericCharacters;
var MinLowercase = securityOption.PasswordMinRequiredLowercase;
var MinUppercase = securityOption.PasswordMinRequiredUppercase;
//LenghtCheck
if (passwordChecker.length < MinLength);
return false;
if (!CountSpecialChars(passwordChecker) > SpecialChars) {
return false;
}
if (MinLowercase > 0) {
if (!CountLowerCase(passwordChecker) > MinLowercase) {
return false;
}
}
if (MinUppercase > 0) {
if (!CountUpperCase(passwordChecker) > MinLowercase) {
return false;
}
}
}
function CountSpecialChars(text) {
var Count = 0;
for (var i = 0; i < text.length; i++) {
var c = text[i];
if (text[i] >= 33 && text[i] <= 63){
Count++;
}
}
}
function MinLowercase(text) {
var Count = 0;
for (var i = 0; i < text.length; i++) {
var c = text[i];
if (text[i] >= 97 && text[i] <= 122) {
Count++;
}
}
}
function MinUppercase(text) {
var Count = 0;
for (var i = 0; i < text.length; i++) {
var c = text[i];
if (text[i] >= 65 && text[i] <= 90) {
Count++;
}
}
}
Now what I want to do is, check the different conditions as a whole and if all of the conditions are true then change the class to green..
$(pageId + ' #password').bind('keyup', function () {
var currentpassword = $(pageId + ' #password').val();
if (isStrong(currentpassword)) {
$(pageId + ' #password').addClass('green');
} else {
$(pageId + ' #password').addClass('red');
}
});
I am not sure how to check the conditions as a whole and return an overall true because as I start trying in my password it instantly changes to green as in my password spec you do not need any UpperCase or LowerCase letters so on any input of a char it returns true..
You should refactor your functions so that they accept both the string and the parameter and return true or false. For example:
function CountSpecialChars(text) {
var Count = 0;
for (var i = 0; i < text.length; i++) {
var c = text[i];
if (text[i] >= 33 && text[i] <= 63){
Count++;
}
}
}
if (!CountSpecialChars(passwordChecker) > SpecialChars) {
return false;
}
Should instead be:
function CountSpecialChars(text, min) {
var count = 0;
for (var i = 0; i < text.length; i++) {
var c = text[i];
if (text[i] >= 33 && text[i] <= 63){
count++;
}
}
return count > min;
}
return CountSpecialChars(passwordChecker, SpecialChars);
Also, as a bonus, you could also avoid that for loop for those functions by using replace, like so:
function MinChars(text, min) {
return text.length > min;
}
function MinUppercase(text, min) {
var non_uppers = /[^A-Z]/g;
var uppers = text.replace(non_uppers, text);
return uppers.length > min;
}
function MinLowercase(text, min) {
var non_lowers = /[^a-z]/g;
var lowers = text.replace(non_lowers, text);
return lowers.length > min;
}
function MinSpecialChars(text, min) {
var non_specials = /[^!-\?]/g;
var specials = text.replace(non_specials, text);
return specials.length > min;
}
Now with those functions, you can have:
if !MinChars(pw, MinLength) return false;
if !MinSpecialChars(pw, SpecialChars) return false;
if !MinLowercase(pw, MinLowercase) return false;
if !MinUppercase(pw, MinUppercase) return false;
return true;

Not sure what is wrong with the function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am completely new to this and am trying to write a program which will take inputs on a webpage and score the results in an output box. I am not sure what the problem is with this set of javascript, though I am sure that I am missing an integral piece! Any help is much appreciated!
function laten() {
var Q2 = document.getElementById('twoScore').value;
if (Q2 == "") {
Q2 = 0;}
var Q2new = 0;
if (parseInt(Q2) >= 0) && (parseInt(Q2) <= 15) {
Q2new = 0;
} else if (parseInt(Q2) > 15) && (parseInt(Q2) <=30) {
Q2new = 1;
} else if (parseInt(Q2) > 30) && (parseInt(Q2) <=60) {
Q2new = 2;
} else if (parseInt(Q2) > 60) {
Q2new = 3;
}
document.getElementById('latency').value = Q2new;
var Q5a = document.getElementById('fiveaScore').value;
if (Q5a == "") {
Q5a = 0;}
var latenAdd = parseInt(Q5a) + parseInt(Q2new);
if (latenAdd == "") {
latenAdd = 0;}
var latenScore = 0;
if (latenScore == "") {
latenScore = 0;}
if (latenAdd == 0) {
latenScore = 0;
} else if ((latenAdd >= 1) && (latenAdd <= 2)) {
latenScore = 1;
} else if ((latenAdd >= 3) && (latenAdd <= 4)) {
latenScore = 2;
} else if ((latenAdd >= 1) && (latenAdd <= 2)) {
latenScore = 3;
}
if (!isNaN(latenScore)) {
document.getElementById('latency').value = latenScore;
}
You have several syntax errors:
You are missing global parens in some if statements with multiple conditions, it should be like this:
if ( (condition 1) && (condition2))
You are also missing a final }:
Here is the final fixed code:
function laten() {
var Q2 = document.getElementById('twoScore').value;
if (Q2 == "") {
Q2 = 0;
}
var Q2new = 0;
if ((parseInt(Q2) >= 0) && (parseInt(Q2) <= 15)) {
Q2new = 0;
} else if ((parseInt(Q2) > 15) && (parseInt(Q2) <=30)) {
Q2new = 1;
} else if ((parseInt(Q2) > 30) && (parseInt(Q2) <=60)) {
Q2new = 2;
} else if (parseInt(Q2) > 60) {
Q2new = 3;
}
document.getElementById('latency').value = Q2new;
var Q5a = document.getElementById('fiveaScore').value;
if (Q5a == "") {
Q5a = 0;
}
var latenAdd = parseInt(Q5a) + parseInt(Q2new);
if (latenAdd == "") {
latenAdd = 0;
}
var latenScore = 0;
if (latenScore == "") {
latenScore = 0;
}
if (latenAdd == 0) {
latenScore = 0;
}
else if ((latenAdd >= 1) && (latenAdd <= 2)) {
latenScore = 1;
}
else if ((latenAdd >= 3) && (latenAdd <= 4)) {
latenScore = 2;
}
else if ((latenAdd >= 1) && (latenAdd <= 2)) {
latenScore = 3;
}
if (!isNaN(latenScore)) {
document.getElementById('latency').value = latenScore;
}
}

Categories