React calculator: check if any value exists in string
up vote
0
down vote
favorite
Is there anyway I could check if more than one value exists in a string? My idea is to have only two conditions:
a) if there´s no decimal, add it;
b) if there´s an operator (+-*/) followed by a number from 0 to 9, add it.
The decimal gets added only when one of those two conditions are met. What string method could I use?
handleDecimal(evt){
const result = evt.target.value;
let value = this.state.value;
if (value.indexOf('.') === -1){
this.setState(prevState => ({
value: this.state.value + result
}))};
}
javascript reactjs
add a comment |
up vote
0
down vote
favorite
Is there anyway I could check if more than one value exists in a string? My idea is to have only two conditions:
a) if there´s no decimal, add it;
b) if there´s an operator (+-*/) followed by a number from 0 to 9, add it.
The decimal gets added only when one of those two conditions are met. What string method could I use?
handleDecimal(evt){
const result = evt.target.value;
let value = this.state.value;
if (value.indexOf('.') === -1){
this.setState(prevState => ({
value: this.state.value + result
}))};
}
javascript reactjs
What exactly do you mean, "add it"? Do you mean add some numbers together, or insert a decimal point (at what position?), or what? Can you give a couple of examples?
– CertainPerformance
Nov 11 at 7:03
In the current handle event, if the decimal isn´t there, the decimal gets added. I want something similar to that, but the condition should be something like "set the state with a decimal only if the decimal doesn´t exist already OR if there´s an operator followed by a number". For instance: if the string is "2322399838", you can add a decimal to it (just one). If, after that, there´s a "+2293998" you can add another decimal and so on. Hopefully I´m being clear.
– Hernan Ariel
Nov 11 at 7:07
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Is there anyway I could check if more than one value exists in a string? My idea is to have only two conditions:
a) if there´s no decimal, add it;
b) if there´s an operator (+-*/) followed by a number from 0 to 9, add it.
The decimal gets added only when one of those two conditions are met. What string method could I use?
handleDecimal(evt){
const result = evt.target.value;
let value = this.state.value;
if (value.indexOf('.') === -1){
this.setState(prevState => ({
value: this.state.value + result
}))};
}
javascript reactjs
Is there anyway I could check if more than one value exists in a string? My idea is to have only two conditions:
a) if there´s no decimal, add it;
b) if there´s an operator (+-*/) followed by a number from 0 to 9, add it.
The decimal gets added only when one of those two conditions are met. What string method could I use?
handleDecimal(evt){
const result = evt.target.value;
let value = this.state.value;
if (value.indexOf('.') === -1){
this.setState(prevState => ({
value: this.state.value + result
}))};
}
javascript reactjs
javascript reactjs
asked Nov 11 at 7:01
Hernan Ariel
13216
13216
What exactly do you mean, "add it"? Do you mean add some numbers together, or insert a decimal point (at what position?), or what? Can you give a couple of examples?
– CertainPerformance
Nov 11 at 7:03
In the current handle event, if the decimal isn´t there, the decimal gets added. I want something similar to that, but the condition should be something like "set the state with a decimal only if the decimal doesn´t exist already OR if there´s an operator followed by a number". For instance: if the string is "2322399838", you can add a decimal to it (just one). If, after that, there´s a "+2293998" you can add another decimal and so on. Hopefully I´m being clear.
– Hernan Ariel
Nov 11 at 7:07
add a comment |
What exactly do you mean, "add it"? Do you mean add some numbers together, or insert a decimal point (at what position?), or what? Can you give a couple of examples?
– CertainPerformance
Nov 11 at 7:03
In the current handle event, if the decimal isn´t there, the decimal gets added. I want something similar to that, but the condition should be something like "set the state with a decimal only if the decimal doesn´t exist already OR if there´s an operator followed by a number". For instance: if the string is "2322399838", you can add a decimal to it (just one). If, after that, there´s a "+2293998" you can add another decimal and so on. Hopefully I´m being clear.
– Hernan Ariel
Nov 11 at 7:07
What exactly do you mean, "add it"? Do you mean add some numbers together, or insert a decimal point (at what position?), or what? Can you give a couple of examples?
– CertainPerformance
Nov 11 at 7:03
What exactly do you mean, "add it"? Do you mean add some numbers together, or insert a decimal point (at what position?), or what? Can you give a couple of examples?
– CertainPerformance
Nov 11 at 7:03
In the current handle event, if the decimal isn´t there, the decimal gets added. I want something similar to that, but the condition should be something like "set the state with a decimal only if the decimal doesn´t exist already OR if there´s an operator followed by a number". For instance: if the string is "2322399838", you can add a decimal to it (just one). If, after that, there´s a "+2293998" you can add another decimal and so on. Hopefully I´m being clear.
– Hernan Ariel
Nov 11 at 7:07
In the current handle event, if the decimal isn´t there, the decimal gets added. I want something similar to that, but the condition should be something like "set the state with a decimal only if the decimal doesn´t exist already OR if there´s an operator followed by a number". For instance: if the string is "2322399838", you can add a decimal to it (just one). If, after that, there´s a "+2293998" you can add another decimal and so on. Hopefully I´m being clear.
– Hernan Ariel
Nov 11 at 7:07
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You can use a regular expression and String.prototype.search() to accomplish this.
The RegEx /([-+*/]d)/
will match an operator followed by any digit.
If you want to add a decimal even when there exists a decimal, but there is also an operator followed by a number:
if (value.indexOf === '-1' || value.search(/([-+*/]d)/) > -1) { //add decimal }
If you want to have only one decimal at most in the final string:
if (value.indexOf === '-1') {
//add decimal
} else if (value.search(/([-+*/]d)/) > -1) {
//add decimal
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You can use a regular expression and String.prototype.search() to accomplish this.
The RegEx /([-+*/]d)/
will match an operator followed by any digit.
If you want to add a decimal even when there exists a decimal, but there is also an operator followed by a number:
if (value.indexOf === '-1' || value.search(/([-+*/]d)/) > -1) { //add decimal }
If you want to have only one decimal at most in the final string:
if (value.indexOf === '-1') {
//add decimal
} else if (value.search(/([-+*/]d)/) > -1) {
//add decimal
}
add a comment |
up vote
1
down vote
accepted
You can use a regular expression and String.prototype.search() to accomplish this.
The RegEx /([-+*/]d)/
will match an operator followed by any digit.
If you want to add a decimal even when there exists a decimal, but there is also an operator followed by a number:
if (value.indexOf === '-1' || value.search(/([-+*/]d)/) > -1) { //add decimal }
If you want to have only one decimal at most in the final string:
if (value.indexOf === '-1') {
//add decimal
} else if (value.search(/([-+*/]d)/) > -1) {
//add decimal
}
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You can use a regular expression and String.prototype.search() to accomplish this.
The RegEx /([-+*/]d)/
will match an operator followed by any digit.
If you want to add a decimal even when there exists a decimal, but there is also an operator followed by a number:
if (value.indexOf === '-1' || value.search(/([-+*/]d)/) > -1) { //add decimal }
If you want to have only one decimal at most in the final string:
if (value.indexOf === '-1') {
//add decimal
} else if (value.search(/([-+*/]d)/) > -1) {
//add decimal
}
You can use a regular expression and String.prototype.search() to accomplish this.
The RegEx /([-+*/]d)/
will match an operator followed by any digit.
If you want to add a decimal even when there exists a decimal, but there is also an operator followed by a number:
if (value.indexOf === '-1' || value.search(/([-+*/]d)/) > -1) { //add decimal }
If you want to have only one decimal at most in the final string:
if (value.indexOf === '-1') {
//add decimal
} else if (value.search(/([-+*/]d)/) > -1) {
//add decimal
}
edited Nov 11 at 14:35
answered Nov 11 at 14:25
Syed Rashid
262
262
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246546%2freact-calculator-check-if-any-value-exists-in-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What exactly do you mean, "add it"? Do you mean add some numbers together, or insert a decimal point (at what position?), or what? Can you give a couple of examples?
– CertainPerformance
Nov 11 at 7:03
In the current handle event, if the decimal isn´t there, the decimal gets added. I want something similar to that, but the condition should be something like "set the state with a decimal only if the decimal doesn´t exist already OR if there´s an operator followed by a number". For instance: if the string is "2322399838", you can add a decimal to it (just one). If, after that, there´s a "+2293998" you can add another decimal and so on. Hopefully I´m being clear.
– Hernan Ariel
Nov 11 at 7:07