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.










share|improve this question






















  • 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















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.










share|improve this question






















  • 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













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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












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






share|improve this answer























  • 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


















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` };
}





share|improve this answer



















  • 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




















up vote
1
down vote













Try this



width: (20 - value) + ‘%’






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    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

























    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






    share|improve this answer























    • 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















    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






    share|improve this answer























    • 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













    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






    share|improve this answer














    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







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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


















    • 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












    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` };
    }





    share|improve this answer



















    • 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

















    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` };
    }





    share|improve this answer



















    • 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















    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` };
    }





    share|improve this answer














    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` };
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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
















    • 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












    up vote
    1
    down vote













    Try this



    width: (20 - value) + ‘%’






    share|improve this answer

























      up vote
      1
      down vote













      Try this



      width: (20 - value) + ‘%’






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        Try this



        width: (20 - value) + ‘%’






        share|improve this answer












        Try this



        width: (20 - value) + ‘%’







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 4:53









        Nitish Narang

        2,935812




        2,935812






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings