Variable.match for amount > 999.99 [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
For example :
Var1 = 289.56
I use this formula :
foundStr = Var1.match("[0-9]+\.[0-9]+");
Price( parseFloat(foundStr).toFixed(2) );
But when Var1 > 999.99 (Example : 2,356.21)
What is the script find the string ?
foundStr = Var1.match(??);
Thank you

You already have a numeric variable, why are you messing with strings?
var number1 = 289.56;
if (number1 > 999.99) {
// do whatever
}
If you're trying to round, use Math.floor instead:
var number1 = 289.56485345734593453;
var roundedNumber1 = Math.floor(number1 * 10) / 10; // two decimal points

I think you just want to remove the commas and check if its a float, but its hard to tell based off your question. How about something like this:
var Var1 = "1,234.567";
var parsed = parseFloat(Var1.replace(",",""), 10);
if (isNaN(parsed)) {
// its not a valid number, so deal with it as needed
}
else {
// parsed now holds your Number, so use it
}
This approach will work regardless of if the number is >= 1000.

var Var1 = "2,356.21";
foundStr = String(Var1.match(/([0-9]{1,3},)?[0-9]{0,3}\.[0-9]+/g)).replace(/,/g, "");
var result = parseFloat(foundStr).toFixed(2);

Related

Need to have one string from Looped Chars JS [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
need to get the output as 1 string instead of looped string
the output I got each letter on its own
need to have the second output which is one word
Thanks in advance:D
let start = 0;
let swappedName = "elZerO";
for (let i = start; i<swappedName.length; i++){
if (swappedName[i] == swappedName[i].toUpperCase()) {
console.log(swappedName[i].toLowerCase());
}else {
console.log(swappedName[i].toUpperCase());
}
}
//Output
E
L
z
E
R
o
// Need to be
"ELzERo"
Use string = string0+string1 , or keep adding values to an array, then join the array with array.join()
MasteringJs has a great guide on ways to merge characters and strings.
let start = 0;
let swappedName = "elZerO";
var outputString="";
var outputStringArray=[];
var newChar="";
for (let i = start; i<swappedName.length; i++){
if (swappedName[i] == swappedName[i].toUpperCase()) {
newChar = swappedName[i].toLowerCase();
}else {
newChar=swappedName[i].toUpperCase();
}
outputStringArray.push(newChar);
outputString+=newChar;
}
console.log("[Output using string1 + string 2] is "+outputString); // Another example of concating string
console.log("[Output using array.join] is "+outputStringArray.join("")); // Another example of concating string
// Need to be
"ELzERo"

How to generate an 8 digit string from a given short string in javascript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to generate an 8-digit string from a given short string, with a condition. The condition is that if the given string is less then 8 digits long then add 9 for the missing entries.
Let's say,
the input is 123 the output should be 12399999
if 0123 then 012399999
if 01203 then 01203999
if 1230 then 12309999 and so on...
Thanks in advance.
In TypeScript, so you can see how the value types change (as you're performing String operations on a Number type!)
const someNumber: number = 123;
const asText : string = someNumber.toString();
const padded : string = asText.padEnd( 8, '9' );
const asNumber : number = parseInt( padded, 10 ); // ALWAYS specify the radix!
As a function:
function padNinesNumber( value: number ): number {
const asText : string = value.toString();
const padded : string = asText.padEnd( 8, '9' );
const asNumber : number = parseInt( padded, 10 ); // ALWAYS specify the radix!
}
I'm not sure if this is what you mean,
Try this, I gave the code to add '9' for every difference of 8 with the number of char in the string.:
const addNine = val =>
val.toString()+'9'.repeat(8-val.toString().length);
console.log(addNine('2233'))
console.log(addNine('012303'))
console.log(addNine(1230))
or you can try this if you're not giving number data type
console.log(('2233').padEnd(8,'9'))
console.log(('012303').padEnd(8,'9'))

Get last "ordered" number [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I've been struggling with the definition of a function. It must take a positive number N as a parameter and return the last ordered number. By "ordered" number I mean that every digit follow each other.
Example1 : Takes 1000 as a parameter and returns 789.
Example2 : Takes 500 as a parameter and returns 456.
Here's working code. Although I don't like just giving you the answer. This is something you have to practice and learn how to do on your own. Take the time to understand what I did. Also, this solution can be improved a lot, just something that I did quickly and works.
The algorithm in action:
function calc() {
//Get the number in the text input
var nbr = parseInt(document.getElementById("number").value, 10);
//Loop starting from the top
for (var i = nbr; i > 0; i--) {
if (isOrderedNumber(i)) {
document.getElementById("result").innerText = i;
return;
}
}
document.getElementById("result").innerText = "None found";
}
function isOrderedNumber(number) {
var digits = number.toString().split('');
//Loops in the digits of the number
for (var i = 0; i < digits.length - 1; i++) {
//Check if the current number+1 is not equal to the next number.
if (parseInt(digits[i]) + 1 !== parseInt(digits[i + 1])) {
return false;
}
}
return true;
}
<input id="number" type="text" />
<button onclick="calc()">Find last ordered number</button>
<br/>
<br/>
<span id="result"></span>
In you case, instead of using html element you would receive "nbr" by parameter instead and would return the value instead of putting the value in the html element. Ask if you have any questions on how this works.

Masking number at a particular position [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a input field with a maximum length of 15 character.
i want to do something like this
original number :784198012345671
should be: 784********5671
The process to achieving what you want varies depending on whether:
you want to mask the value after the it has been entered or
you want to mask the value during typing.
If you want to do this, after the value has been entered the following should do the trick.
Snippet:
var
/* The original value. */
n = "784198012345671",
/* The masked value. */
masked = n.slice(0, 3) + Array(8).join("*") + n.slice(-4);
/* Log the result. */
console.log(masked);
If, instead, you want to mask the input during typing, things get more complicated.
Snippet:
$(".newsocial").on("keyup", function(e) {
/* Turn the value into an array of characters. */
var value = this.value.split("");
/* Iterate over every character. */
value.forEach(function (char, index) {
/* Replace the character with a placeholder when appropriate. */
if (index >= 3 && index <= 10) value[index] = "*";
});
/* Turn the array of chars into a string & assign it to the value of the input. */
this.value = value.join("");
})
<!--- HTML --->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" maxlength="15" id="input-card" class="newsocial">
const original = "784198012345671",
obfuscated = original.substr(0, 3) + "*".repeat(original.length - 7) + original.substr(-4);
You may use substr to get the number groups at the beginning and the end out of the number and then use repeat to fill the asterisks inbetween.
Convert your number to string and use string.prototype.substring and string.prototype.repeate to build parts:
var number = 784198012345671;
number = number.toString();
var res = number.substring(0, 3) + '*'.repeat(8) + number.substring(11, 15);
console.log(res);
Replace Integer to String
Iterate the string and replace the position with *:
let number = 123455666777888;
let stringedNum = number.toString();
for(i=0;i<stringedNum.length;i++) {
if(i>5 && i<10) { // change this line as per your need
console.log(i)
stringedNum = stringedNum.replace(stringedNum[i],'*');
}
}
console.log(stringedNum)

Converting from base 10 beginner [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I was asked to write code that helps to go from base 10 to any other base. So far I have this
var y=23 //Number in base 10 I want to convert
var x=2 //Base I want to convert to
var r=[]
if(y>=x){
r.push(y%x)
var y=(Math.floor(y/x))
}
else {
r.push(y)
}
console.log(r)
Sorry if this is to basic i need to keep it simple thanks for the help
Use these codes:
var digits = [
'0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z'
];
/**
* x is the base, y is the decimal value
* x must be in the range of 2..36
* y must be greater than zero
*/
function base10ToBaseN(x,y) {
var str = "";
while (y>0) {
var mod = y%x;
var div = Math.floor(y/x);
str = digits[mod]+str;
y = div;
}
return str;
}
alert(base10ToBaseN(2,23));
See this jsfiddle: http://jsfiddle.net/jondinham/mLz5ycey
function convertToBase(input, base){
var digits = [];
while(digits.push(letter(input%base)), input = (input/base)|0) //work for int
function letter(n){ return n > 9 ? String.fromCharCode(n%base+55) : n;}
return digits.reverse().join("");
}
convertToBase(255,16); //FF
convertToBase(255,2); //11111111
You can even attach this to the Number prototype for extra fun.
Number.prototype._ = function(b){ return convertToBase(this, b); };
255.._(16); //FF
255.._(2); //11111111
//Doesn't look like JavaScript's syntax anymore :)
However note that this only work for ∀n ∈ ℤ, n ≥ 0. Demo: http://jsfiddle.net/DerekL/nxf6wpkz/
Why do mathematicians claim that Halloween is the same as Christmas?
Because convertToBase(25, 8).
Oct 31 = Dec 25

Categories

Resources