calculation of increment value in react js
up vote
0
down vote
favorite
I want to pass the parameter value in style for calculate my width. my code is
function getValue(value) {
if (value === 12) {
return {
width: '( 20 - value )%',
};
}
return false;
}
but the width does not working. I am new to react. please help.
javascript reactjs electron
add a comment |
up vote
0
down vote
favorite
I want to pass the parameter value in style for calculate my width. my code is
function getValue(value) {
if (value === 12) {
return {
width: '( 20 - value )%',
};
}
return false;
}
but the width does not working. I am new to react. please help.
javascript reactjs electron
I'm confused? You just want to get (20 - value)%?
– Electrox Mortem
Nov 9 at 4:45
yes @JermahlWhite
– jan
Nov 9 at 4:47
so if the value is 12 what do you want the function to return? because otherwise it will return false
– JSONaLeo
Nov 9 at 4:51
If it only returns when it's 12, you can just return {width:0.08} whenever it's 12 instead of (20 - value)/100
– Electrox Mortem
Nov 9 at 4:57
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to pass the parameter value in style for calculate my width. my code is
function getValue(value) {
if (value === 12) {
return {
width: '( 20 - value )%',
};
}
return false;
}
but the width does not working. I am new to react. please help.
javascript reactjs electron
I want to pass the parameter value in style for calculate my width. my code is
function getValue(value) {
if (value === 12) {
return {
width: '( 20 - value )%',
};
}
return false;
}
but the width does not working. I am new to react. please help.
javascript reactjs electron
javascript reactjs electron
asked Nov 9 at 4:42
jan
7011
7011
I'm confused? You just want to get (20 - value)%?
– Electrox Mortem
Nov 9 at 4:45
yes @JermahlWhite
– jan
Nov 9 at 4:47
so if the value is 12 what do you want the function to return? because otherwise it will return false
– JSONaLeo
Nov 9 at 4:51
If it only returns when it's 12, you can just return {width:0.08} whenever it's 12 instead of (20 - value)/100
– Electrox Mortem
Nov 9 at 4:57
add a comment |
I'm confused? You just want to get (20 - value)%?
– Electrox Mortem
Nov 9 at 4:45
yes @JermahlWhite
– jan
Nov 9 at 4:47
so if the value is 12 what do you want the function to return? because otherwise it will return false
– JSONaLeo
Nov 9 at 4:51
If it only returns when it's 12, you can just return {width:0.08} whenever it's 12 instead of (20 - value)/100
– Electrox Mortem
Nov 9 at 4:57
I'm confused? You just want to get (20 - value)%?
– Electrox Mortem
Nov 9 at 4:45
I'm confused? You just want to get (20 - value)%?
– Electrox Mortem
Nov 9 at 4:45
yes @JermahlWhite
– jan
Nov 9 at 4:47
yes @JermahlWhite
– jan
Nov 9 at 4:47
so if the value is 12 what do you want the function to return? because otherwise it will return false
– JSONaLeo
Nov 9 at 4:51
so if the value is 12 what do you want the function to return? because otherwise it will return false
– JSONaLeo
Nov 9 at 4:51
If it only returns when it's 12, you can just return {width:0.08} whenever it's 12 instead of (20 - value)/100
– Electrox Mortem
Nov 9 at 4:57
If it only returns when it's 12, you can just return {width:0.08} whenever it's 12 instead of (20 - value)/100
– Electrox Mortem
Nov 9 at 4:57
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
You can't calculate if it's a string. Quote marks ('') make it a string. Also, % is a remainder operator. It gets the remainder of two numbers. It's similar to division. You can read more about the remainder operator on MDN
I don't understand what this has to do with react, this is more just vanilla javascript.
I think what you may want is
function getValue(value) {
if (value !== 12) { return false; }
return {
width: (20 - value)/100,
};
}
You could also do the following since it only returns if the value if 12
var getValue = value => value === 12 ? { width: 0.08 } : false
This solution uses arrow functions, auto return, and ternary operators. You can read about them on mdn
Upvote from me too for thinking in smart way :)
– Hemadri Dasari
Nov 9 at 5:33
free vote from me :) thanks for calling out remainder and also linking to MDN!
– John Ruddell
Nov 9 at 5:33
@JohnRuddell wat did you edit?
– Electrox Mortem
Nov 9 at 6:03
I feel violated. Edited without my knowing
– Electrox Mortem
Nov 9 at 6:04
Haha! I helped didn't I? :)
– John Ruddell
Nov 9 at 6:28
|
show 2 more comments
up vote
2
down vote
You need something like below. You are checking a constant value, so no need to do the arithmetic, just return 8. You can simplify the function as like below
function getValue(value) {
// return object for style or undefined for no styling
return value === 12 ? {'width': '8% !important' } : undefined;
}
If you were to keep the arithmetic because you wanted to just always do a calculation (for instance). Then a nicer way to write out the function would be like this.
function getValue(value) {
return { 'width': `${20 - value}% !important` };
}
1
Why even use a second line? You could just check to see if it's 12, and if it is, return 0.08? since (20-12)% is always 8%
– Electrox Mortem
Nov 9 at 4:59
Agree with you :)
– Hemadri Dasari
Nov 9 at 5:13
1
Free upvote because you agree with me and also because of quality. Definitely mostly because quality ;)
– Electrox Mortem
Nov 9 at 5:25
1
Free upvote and edits from me :) nice way to "think twice" ;) about the attempt!
– John Ruddell
Nov 9 at 5:27
1
@Jermahl White That was good catch man. Thank you
– Hemadri Dasari
Nov 9 at 5:27
|
show 2 more comments
up vote
1
down vote
Try this
width: (20 - value) + ‘%’
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You can't calculate if it's a string. Quote marks ('') make it a string. Also, % is a remainder operator. It gets the remainder of two numbers. It's similar to division. You can read more about the remainder operator on MDN
I don't understand what this has to do with react, this is more just vanilla javascript.
I think what you may want is
function getValue(value) {
if (value !== 12) { return false; }
return {
width: (20 - value)/100,
};
}
You could also do the following since it only returns if the value if 12
var getValue = value => value === 12 ? { width: 0.08 } : false
This solution uses arrow functions, auto return, and ternary operators. You can read about them on mdn
Upvote from me too for thinking in smart way :)
– Hemadri Dasari
Nov 9 at 5:33
free vote from me :) thanks for calling out remainder and also linking to MDN!
– John Ruddell
Nov 9 at 5:33
@JohnRuddell wat did you edit?
– Electrox Mortem
Nov 9 at 6:03
I feel violated. Edited without my knowing
– Electrox Mortem
Nov 9 at 6:04
Haha! I helped didn't I? :)
– John Ruddell
Nov 9 at 6:28
|
show 2 more comments
up vote
2
down vote
You can't calculate if it's a string. Quote marks ('') make it a string. Also, % is a remainder operator. It gets the remainder of two numbers. It's similar to division. You can read more about the remainder operator on MDN
I don't understand what this has to do with react, this is more just vanilla javascript.
I think what you may want is
function getValue(value) {
if (value !== 12) { return false; }
return {
width: (20 - value)/100,
};
}
You could also do the following since it only returns if the value if 12
var getValue = value => value === 12 ? { width: 0.08 } : false
This solution uses arrow functions, auto return, and ternary operators. You can read about them on mdn
Upvote from me too for thinking in smart way :)
– Hemadri Dasari
Nov 9 at 5:33
free vote from me :) thanks for calling out remainder and also linking to MDN!
– John Ruddell
Nov 9 at 5:33
@JohnRuddell wat did you edit?
– Electrox Mortem
Nov 9 at 6:03
I feel violated. Edited without my knowing
– Electrox Mortem
Nov 9 at 6:04
Haha! I helped didn't I? :)
– John Ruddell
Nov 9 at 6:28
|
show 2 more comments
up vote
2
down vote
up vote
2
down vote
You can't calculate if it's a string. Quote marks ('') make it a string. Also, % is a remainder operator. It gets the remainder of two numbers. It's similar to division. You can read more about the remainder operator on MDN
I don't understand what this has to do with react, this is more just vanilla javascript.
I think what you may want is
function getValue(value) {
if (value !== 12) { return false; }
return {
width: (20 - value)/100,
};
}
You could also do the following since it only returns if the value if 12
var getValue = value => value === 12 ? { width: 0.08 } : false
This solution uses arrow functions, auto return, and ternary operators. You can read about them on mdn
You can't calculate if it's a string. Quote marks ('') make it a string. Also, % is a remainder operator. It gets the remainder of two numbers. It's similar to division. You can read more about the remainder operator on MDN
I don't understand what this has to do with react, this is more just vanilla javascript.
I think what you may want is
function getValue(value) {
if (value !== 12) { return false; }
return {
width: (20 - value)/100,
};
}
You could also do the following since it only returns if the value if 12
var getValue = value => value === 12 ? { width: 0.08 } : false
This solution uses arrow functions, auto return, and ternary operators. You can read about them on mdn
edited Nov 9 at 5:33
John Ruddell
15.9k43158
15.9k43158
answered Nov 9 at 4:47
Electrox Mortem
15413
15413
Upvote from me too for thinking in smart way :)
– Hemadri Dasari
Nov 9 at 5:33
free vote from me :) thanks for calling out remainder and also linking to MDN!
– John Ruddell
Nov 9 at 5:33
@JohnRuddell wat did you edit?
– Electrox Mortem
Nov 9 at 6:03
I feel violated. Edited without my knowing
– Electrox Mortem
Nov 9 at 6:04
Haha! I helped didn't I? :)
– John Ruddell
Nov 9 at 6:28
|
show 2 more comments
Upvote from me too for thinking in smart way :)
– Hemadri Dasari
Nov 9 at 5:33
free vote from me :) thanks for calling out remainder and also linking to MDN!
– John Ruddell
Nov 9 at 5:33
@JohnRuddell wat did you edit?
– Electrox Mortem
Nov 9 at 6:03
I feel violated. Edited without my knowing
– Electrox Mortem
Nov 9 at 6:04
Haha! I helped didn't I? :)
– John Ruddell
Nov 9 at 6:28
Upvote from me too for thinking in smart way :)
– Hemadri Dasari
Nov 9 at 5:33
Upvote from me too for thinking in smart way :)
– Hemadri Dasari
Nov 9 at 5:33
free vote from me :) thanks for calling out remainder and also linking to MDN!
– John Ruddell
Nov 9 at 5:33
free vote from me :) thanks for calling out remainder and also linking to MDN!
– John Ruddell
Nov 9 at 5:33
@JohnRuddell wat did you edit?
– Electrox Mortem
Nov 9 at 6:03
@JohnRuddell wat did you edit?
– Electrox Mortem
Nov 9 at 6:03
I feel violated. Edited without my knowing
– Electrox Mortem
Nov 9 at 6:04
I feel violated. Edited without my knowing
– Electrox Mortem
Nov 9 at 6:04
Haha! I helped didn't I? :)
– John Ruddell
Nov 9 at 6:28
Haha! I helped didn't I? :)
– John Ruddell
Nov 9 at 6:28
|
show 2 more comments
up vote
2
down vote
You need something like below. You are checking a constant value, so no need to do the arithmetic, just return 8. You can simplify the function as like below
function getValue(value) {
// return object for style or undefined for no styling
return value === 12 ? {'width': '8% !important' } : undefined;
}
If you were to keep the arithmetic because you wanted to just always do a calculation (for instance). Then a nicer way to write out the function would be like this.
function getValue(value) {
return { 'width': `${20 - value}% !important` };
}
1
Why even use a second line? You could just check to see if it's 12, and if it is, return 0.08? since (20-12)% is always 8%
– Electrox Mortem
Nov 9 at 4:59
Agree with you :)
– Hemadri Dasari
Nov 9 at 5:13
1
Free upvote because you agree with me and also because of quality. Definitely mostly because quality ;)
– Electrox Mortem
Nov 9 at 5:25
1
Free upvote and edits from me :) nice way to "think twice" ;) about the attempt!
– John Ruddell
Nov 9 at 5:27
1
@Jermahl White That was good catch man. Thank you
– Hemadri Dasari
Nov 9 at 5:27
|
show 2 more comments
up vote
2
down vote
You need something like below. You are checking a constant value, so no need to do the arithmetic, just return 8. You can simplify the function as like below
function getValue(value) {
// return object for style or undefined for no styling
return value === 12 ? {'width': '8% !important' } : undefined;
}
If you were to keep the arithmetic because you wanted to just always do a calculation (for instance). Then a nicer way to write out the function would be like this.
function getValue(value) {
return { 'width': `${20 - value}% !important` };
}
1
Why even use a second line? You could just check to see if it's 12, and if it is, return 0.08? since (20-12)% is always 8%
– Electrox Mortem
Nov 9 at 4:59
Agree with you :)
– Hemadri Dasari
Nov 9 at 5:13
1
Free upvote because you agree with me and also because of quality. Definitely mostly because quality ;)
– Electrox Mortem
Nov 9 at 5:25
1
Free upvote and edits from me :) nice way to "think twice" ;) about the attempt!
– John Ruddell
Nov 9 at 5:27
1
@Jermahl White That was good catch man. Thank you
– Hemadri Dasari
Nov 9 at 5:27
|
show 2 more comments
up vote
2
down vote
up vote
2
down vote
You need something like below. You are checking a constant value, so no need to do the arithmetic, just return 8. You can simplify the function as like below
function getValue(value) {
// return object for style or undefined for no styling
return value === 12 ? {'width': '8% !important' } : undefined;
}
If you were to keep the arithmetic because you wanted to just always do a calculation (for instance). Then a nicer way to write out the function would be like this.
function getValue(value) {
return { 'width': `${20 - value}% !important` };
}
You need something like below. You are checking a constant value, so no need to do the arithmetic, just return 8. You can simplify the function as like below
function getValue(value) {
// return object for style or undefined for no styling
return value === 12 ? {'width': '8% !important' } : undefined;
}
If you were to keep the arithmetic because you wanted to just always do a calculation (for instance). Then a nicer way to write out the function would be like this.
function getValue(value) {
return { 'width': `${20 - value}% !important` };
}
edited Nov 9 at 5:53
answered Nov 9 at 4:56
Hemadri Dasari
6,83911039
6,83911039
1
Why even use a second line? You could just check to see if it's 12, and if it is, return 0.08? since (20-12)% is always 8%
– Electrox Mortem
Nov 9 at 4:59
Agree with you :)
– Hemadri Dasari
Nov 9 at 5:13
1
Free upvote because you agree with me and also because of quality. Definitely mostly because quality ;)
– Electrox Mortem
Nov 9 at 5:25
1
Free upvote and edits from me :) nice way to "think twice" ;) about the attempt!
– John Ruddell
Nov 9 at 5:27
1
@Jermahl White That was good catch man. Thank you
– Hemadri Dasari
Nov 9 at 5:27
|
show 2 more comments
1
Why even use a second line? You could just check to see if it's 12, and if it is, return 0.08? since (20-12)% is always 8%
– Electrox Mortem
Nov 9 at 4:59
Agree with you :)
– Hemadri Dasari
Nov 9 at 5:13
1
Free upvote because you agree with me and also because of quality. Definitely mostly because quality ;)
– Electrox Mortem
Nov 9 at 5:25
1
Free upvote and edits from me :) nice way to "think twice" ;) about the attempt!
– John Ruddell
Nov 9 at 5:27
1
@Jermahl White That was good catch man. Thank you
– Hemadri Dasari
Nov 9 at 5:27
1
1
Why even use a second line? You could just check to see if it's 12, and if it is, return 0.08? since (20-12)% is always 8%
– Electrox Mortem
Nov 9 at 4:59
Why even use a second line? You could just check to see if it's 12, and if it is, return 0.08? since (20-12)% is always 8%
– Electrox Mortem
Nov 9 at 4:59
Agree with you :)
– Hemadri Dasari
Nov 9 at 5:13
Agree with you :)
– Hemadri Dasari
Nov 9 at 5:13
1
1
Free upvote because you agree with me and also because of quality. Definitely mostly because quality ;)
– Electrox Mortem
Nov 9 at 5:25
Free upvote because you agree with me and also because of quality. Definitely mostly because quality ;)
– Electrox Mortem
Nov 9 at 5:25
1
1
Free upvote and edits from me :) nice way to "think twice" ;) about the attempt!
– John Ruddell
Nov 9 at 5:27
Free upvote and edits from me :) nice way to "think twice" ;) about the attempt!
– John Ruddell
Nov 9 at 5:27
1
1
@Jermahl White That was good catch man. Thank you
– Hemadri Dasari
Nov 9 at 5:27
@Jermahl White That was good catch man. Thank you
– Hemadri Dasari
Nov 9 at 5:27
|
show 2 more comments
up vote
1
down vote
Try this
width: (20 - value) + ‘%’
add a comment |
up vote
1
down vote
Try this
width: (20 - value) + ‘%’
add a comment |
up vote
1
down vote
up vote
1
down vote
Try this
width: (20 - value) + ‘%’
Try this
width: (20 - value) + ‘%’
answered Nov 9 at 4:53
Nitish Narang
2,935812
2,935812
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53219979%2fcalculation-of-increment-value-in-react-js%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
I'm confused? You just want to get (20 - value)%?
– Electrox Mortem
Nov 9 at 4:45
yes @JermahlWhite
– jan
Nov 9 at 4:47
so if the value is 12 what do you want the function to return? because otherwise it will return false
– JSONaLeo
Nov 9 at 4:51
If it only returns when it's 12, you can just return {width:0.08} whenever it's 12 instead of (20 - value)/100
– Electrox Mortem
Nov 9 at 4:57