JavaScript math calculation case












0














I need to do a JavaScript calculation which goes something like this:



1-500,000 points $30/100,000 points



500,001-999,999 points $25/100,000 points



1,000,000+ points $20/100,000 points



I have case switch which will check the value and then get the correct sum but I want to know how I can do for example if user enter 4,500,000 points or some random value which is more then 1,100,000.



This is what I have currently:



  if (Number(amountOfPoints) > Number(0)) {
switch (true) {
case (amountOfPoints > 0 && amountOfPoints <= 100000):
{
price = 30;
break;
}
case (amountOfPoints > 100000 && amountOfPoints <= 200000):
{
price = 60;
break;
}
case (amountOfPoints > 200000 && amountOfPoints <= 300000):
{
price = 90;
break;
}
case (amountOfPoints > 300000 && amountOfPoints <= 400000):
{
price = 120;
break;
}
case (amountOfPoints > 400000 && amountOfPoints < 500000):
{
price = 150;
break;
}
case (amountOfPoints > 500000 && amountOfPoints < 600000):
{
price = 150;
break;
}
case (amountOfPoints > 600000 && amountOfPoints < 700000):
{
price = 160;
break;
}
case (amountOfPoints > 700000 && amountOfPoints < 800000):
{
price = 185;
break;
}
case (amountOfPoints > 800000 && amountOfPoints < 900000):
{
price = 210;
break;
}
case (amountOfPoints > 900000 && amountOfPoints < 1000000):
{
price = 235;
break;
}
case (amountOfPoints <= 1000000):
{
tweetsPrice = 200;
break;
}
default:
price = 0;
break;
}
}


How can I calculate if points is > 1,100,000? It should increment by $20 for each 100,000.



Update



Perhaps it was not clear what I need. Here is what I need: once the value is over 1,100,000 the price to be calculated accordingly, for example for points between 1,100,001 and 1,200,000 price will be $220, for points between 1,200,001 and 1,300,000 will be $240, etc, without limit, so for example for 4,500,001 and 4,600,000 price will be $720.










share|improve this question




















  • 1




    Why the jQuery tag? I see none.
    – j08691
    Feb 6 '14 at 1:12






  • 1




    @j08691 It's there in spirit
    – DiegoSalazar
    Feb 6 '14 at 1:14










  • Seems like an if statement is in order, and you don't need to do both "sides" of the comparison.
    – Dave Newton
    Feb 6 '14 at 1:17
















0














I need to do a JavaScript calculation which goes something like this:



1-500,000 points $30/100,000 points



500,001-999,999 points $25/100,000 points



1,000,000+ points $20/100,000 points



I have case switch which will check the value and then get the correct sum but I want to know how I can do for example if user enter 4,500,000 points or some random value which is more then 1,100,000.



This is what I have currently:



  if (Number(amountOfPoints) > Number(0)) {
switch (true) {
case (amountOfPoints > 0 && amountOfPoints <= 100000):
{
price = 30;
break;
}
case (amountOfPoints > 100000 && amountOfPoints <= 200000):
{
price = 60;
break;
}
case (amountOfPoints > 200000 && amountOfPoints <= 300000):
{
price = 90;
break;
}
case (amountOfPoints > 300000 && amountOfPoints <= 400000):
{
price = 120;
break;
}
case (amountOfPoints > 400000 && amountOfPoints < 500000):
{
price = 150;
break;
}
case (amountOfPoints > 500000 && amountOfPoints < 600000):
{
price = 150;
break;
}
case (amountOfPoints > 600000 && amountOfPoints < 700000):
{
price = 160;
break;
}
case (amountOfPoints > 700000 && amountOfPoints < 800000):
{
price = 185;
break;
}
case (amountOfPoints > 800000 && amountOfPoints < 900000):
{
price = 210;
break;
}
case (amountOfPoints > 900000 && amountOfPoints < 1000000):
{
price = 235;
break;
}
case (amountOfPoints <= 1000000):
{
tweetsPrice = 200;
break;
}
default:
price = 0;
break;
}
}


How can I calculate if points is > 1,100,000? It should increment by $20 for each 100,000.



Update



Perhaps it was not clear what I need. Here is what I need: once the value is over 1,100,000 the price to be calculated accordingly, for example for points between 1,100,001 and 1,200,000 price will be $220, for points between 1,200,001 and 1,300,000 will be $240, etc, without limit, so for example for 4,500,001 and 4,600,000 price will be $720.










share|improve this question




















  • 1




    Why the jQuery tag? I see none.
    – j08691
    Feb 6 '14 at 1:12






  • 1




    @j08691 It's there in spirit
    – DiegoSalazar
    Feb 6 '14 at 1:14










  • Seems like an if statement is in order, and you don't need to do both "sides" of the comparison.
    – Dave Newton
    Feb 6 '14 at 1:17














0












0








0


1





I need to do a JavaScript calculation which goes something like this:



1-500,000 points $30/100,000 points



500,001-999,999 points $25/100,000 points



1,000,000+ points $20/100,000 points



I have case switch which will check the value and then get the correct sum but I want to know how I can do for example if user enter 4,500,000 points or some random value which is more then 1,100,000.



This is what I have currently:



  if (Number(amountOfPoints) > Number(0)) {
switch (true) {
case (amountOfPoints > 0 && amountOfPoints <= 100000):
{
price = 30;
break;
}
case (amountOfPoints > 100000 && amountOfPoints <= 200000):
{
price = 60;
break;
}
case (amountOfPoints > 200000 && amountOfPoints <= 300000):
{
price = 90;
break;
}
case (amountOfPoints > 300000 && amountOfPoints <= 400000):
{
price = 120;
break;
}
case (amountOfPoints > 400000 && amountOfPoints < 500000):
{
price = 150;
break;
}
case (amountOfPoints > 500000 && amountOfPoints < 600000):
{
price = 150;
break;
}
case (amountOfPoints > 600000 && amountOfPoints < 700000):
{
price = 160;
break;
}
case (amountOfPoints > 700000 && amountOfPoints < 800000):
{
price = 185;
break;
}
case (amountOfPoints > 800000 && amountOfPoints < 900000):
{
price = 210;
break;
}
case (amountOfPoints > 900000 && amountOfPoints < 1000000):
{
price = 235;
break;
}
case (amountOfPoints <= 1000000):
{
tweetsPrice = 200;
break;
}
default:
price = 0;
break;
}
}


How can I calculate if points is > 1,100,000? It should increment by $20 for each 100,000.



Update



Perhaps it was not clear what I need. Here is what I need: once the value is over 1,100,000 the price to be calculated accordingly, for example for points between 1,100,001 and 1,200,000 price will be $220, for points between 1,200,001 and 1,300,000 will be $240, etc, without limit, so for example for 4,500,001 and 4,600,000 price will be $720.










share|improve this question















I need to do a JavaScript calculation which goes something like this:



1-500,000 points $30/100,000 points



500,001-999,999 points $25/100,000 points



1,000,000+ points $20/100,000 points



I have case switch which will check the value and then get the correct sum but I want to know how I can do for example if user enter 4,500,000 points or some random value which is more then 1,100,000.



This is what I have currently:



  if (Number(amountOfPoints) > Number(0)) {
switch (true) {
case (amountOfPoints > 0 && amountOfPoints <= 100000):
{
price = 30;
break;
}
case (amountOfPoints > 100000 && amountOfPoints <= 200000):
{
price = 60;
break;
}
case (amountOfPoints > 200000 && amountOfPoints <= 300000):
{
price = 90;
break;
}
case (amountOfPoints > 300000 && amountOfPoints <= 400000):
{
price = 120;
break;
}
case (amountOfPoints > 400000 && amountOfPoints < 500000):
{
price = 150;
break;
}
case (amountOfPoints > 500000 && amountOfPoints < 600000):
{
price = 150;
break;
}
case (amountOfPoints > 600000 && amountOfPoints < 700000):
{
price = 160;
break;
}
case (amountOfPoints > 700000 && amountOfPoints < 800000):
{
price = 185;
break;
}
case (amountOfPoints > 800000 && amountOfPoints < 900000):
{
price = 210;
break;
}
case (amountOfPoints > 900000 && amountOfPoints < 1000000):
{
price = 235;
break;
}
case (amountOfPoints <= 1000000):
{
tweetsPrice = 200;
break;
}
default:
price = 0;
break;
}
}


How can I calculate if points is > 1,100,000? It should increment by $20 for each 100,000.



Update



Perhaps it was not clear what I need. Here is what I need: once the value is over 1,100,000 the price to be calculated accordingly, for example for points between 1,100,001 and 1,200,000 price will be $220, for points between 1,200,001 and 1,300,000 will be $240, etc, without limit, so for example for 4,500,001 and 4,600,000 price will be $720.







javascript jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 22:08









halfer

14.3k758108




14.3k758108










asked Feb 6 '14 at 1:03









Laziale

2,98132105190




2,98132105190








  • 1




    Why the jQuery tag? I see none.
    – j08691
    Feb 6 '14 at 1:12






  • 1




    @j08691 It's there in spirit
    – DiegoSalazar
    Feb 6 '14 at 1:14










  • Seems like an if statement is in order, and you don't need to do both "sides" of the comparison.
    – Dave Newton
    Feb 6 '14 at 1:17














  • 1




    Why the jQuery tag? I see none.
    – j08691
    Feb 6 '14 at 1:12






  • 1




    @j08691 It's there in spirit
    – DiegoSalazar
    Feb 6 '14 at 1:14










  • Seems like an if statement is in order, and you don't need to do both "sides" of the comparison.
    – Dave Newton
    Feb 6 '14 at 1:17








1




1




Why the jQuery tag? I see none.
– j08691
Feb 6 '14 at 1:12




Why the jQuery tag? I see none.
– j08691
Feb 6 '14 at 1:12




1




1




@j08691 It's there in spirit
– DiegoSalazar
Feb 6 '14 at 1:14




@j08691 It's there in spirit
– DiegoSalazar
Feb 6 '14 at 1:14












Seems like an if statement is in order, and you don't need to do both "sides" of the comparison.
– Dave Newton
Feb 6 '14 at 1:17




Seems like an if statement is in order, and you don't need to do both "sides" of the comparison.
– Dave Newton
Feb 6 '14 at 1:17












3 Answers
3






active

oldest

votes


















0














Usually, it works like this:



function calcPrice(value) {
if (value <= 500000) {
return 30 * Math.ceil(value / 100000);
} else if (value <= 1000000) {
return 150 + 25 * Math.ceil((value - 500000) / 100000);
} else {
return 275 + 20 * Math.ceil((value - 1000000) / 100000);
}
}


but going by the letter of your post:



function calcPrice(value) {
if (value <= 500000) {
return 30 * Math.ceil(value / 100000);
} else if (value <= 1000000) {
return 25 * Math.ceil(value / 100000);
} else {
return 20 * Math.ceil(value / 100000);
}
}





share|improve this answer































    0














    Here is the jsfiddle. You can type in the points and see the rewards value.



    Your point system seems arbitrary so I changed to always start on 1 rather than sometime 0 and sometimes 1. If you don't like it, then you can change the function.



    function points(inText) {
    var inNum = (isNaN(Number(inText))) ? 0 : Math.floor(Number(inText));
    inNum = Math.floor(inNum / 100000);
    if (inNum < 5) {
    inNum = inNum * 30;
    } else {
    if (inNum < 10) {
    inNum = (inNum - 5) * 25 + 150;
    } else {
    inNum = (inNum - 10) * 20 + 275;
    }
    }
    return inNum;
    }





    share|improve this answer





























      -1














      I'm a little tired but I think this might work for any amountOfPoints:



      var price = 30,
      strPoints = amountOfPoints.toString(),
      points = Number(amountOfPoints);

      if (points >= 100000) {
      var multiplier = Number(points.toString().substring(0, 5 - strPoints.length));
      price += 20 * multiplier;
      }


      Basically just doing price = 30 + 20 * the_significant_digits_of_amountOfPoints where amountOfPoints is greater than 100,000. The substring part gives you 1 when there's 6 digits, 2 when there's 7, and so on.



      This should work for the use case "It should increment by $20 for each 100,000". But your case statements assign values to price that don't follow this pattern. So you'd have to use this code after your points are large enough to surpass the conditions in those.






      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',
        autoActivateHeartbeat: false,
        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%2f21591990%2fjavascript-math-calculation-case%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









        0














        Usually, it works like this:



        function calcPrice(value) {
        if (value <= 500000) {
        return 30 * Math.ceil(value / 100000);
        } else if (value <= 1000000) {
        return 150 + 25 * Math.ceil((value - 500000) / 100000);
        } else {
        return 275 + 20 * Math.ceil((value - 1000000) / 100000);
        }
        }


        but going by the letter of your post:



        function calcPrice(value) {
        if (value <= 500000) {
        return 30 * Math.ceil(value / 100000);
        } else if (value <= 1000000) {
        return 25 * Math.ceil(value / 100000);
        } else {
        return 20 * Math.ceil(value / 100000);
        }
        }





        share|improve this answer




























          0














          Usually, it works like this:



          function calcPrice(value) {
          if (value <= 500000) {
          return 30 * Math.ceil(value / 100000);
          } else if (value <= 1000000) {
          return 150 + 25 * Math.ceil((value - 500000) / 100000);
          } else {
          return 275 + 20 * Math.ceil((value - 1000000) / 100000);
          }
          }


          but going by the letter of your post:



          function calcPrice(value) {
          if (value <= 500000) {
          return 30 * Math.ceil(value / 100000);
          } else if (value <= 1000000) {
          return 25 * Math.ceil(value / 100000);
          } else {
          return 20 * Math.ceil(value / 100000);
          }
          }





          share|improve this answer


























            0












            0








            0






            Usually, it works like this:



            function calcPrice(value) {
            if (value <= 500000) {
            return 30 * Math.ceil(value / 100000);
            } else if (value <= 1000000) {
            return 150 + 25 * Math.ceil((value - 500000) / 100000);
            } else {
            return 275 + 20 * Math.ceil((value - 1000000) / 100000);
            }
            }


            but going by the letter of your post:



            function calcPrice(value) {
            if (value <= 500000) {
            return 30 * Math.ceil(value / 100000);
            } else if (value <= 1000000) {
            return 25 * Math.ceil(value / 100000);
            } else {
            return 20 * Math.ceil(value / 100000);
            }
            }





            share|improve this answer














            Usually, it works like this:



            function calcPrice(value) {
            if (value <= 500000) {
            return 30 * Math.ceil(value / 100000);
            } else if (value <= 1000000) {
            return 150 + 25 * Math.ceil((value - 500000) / 100000);
            } else {
            return 275 + 20 * Math.ceil((value - 1000000) / 100000);
            }
            }


            but going by the letter of your post:



            function calcPrice(value) {
            if (value <= 500000) {
            return 30 * Math.ceil(value / 100000);
            } else if (value <= 1000000) {
            return 25 * Math.ceil(value / 100000);
            } else {
            return 20 * Math.ceil(value / 100000);
            }
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 6 '14 at 2:56

























            answered Feb 6 '14 at 2:32









            Igor

            14.4k11824




            14.4k11824

























                0














                Here is the jsfiddle. You can type in the points and see the rewards value.



                Your point system seems arbitrary so I changed to always start on 1 rather than sometime 0 and sometimes 1. If you don't like it, then you can change the function.



                function points(inText) {
                var inNum = (isNaN(Number(inText))) ? 0 : Math.floor(Number(inText));
                inNum = Math.floor(inNum / 100000);
                if (inNum < 5) {
                inNum = inNum * 30;
                } else {
                if (inNum < 10) {
                inNum = (inNum - 5) * 25 + 150;
                } else {
                inNum = (inNum - 10) * 20 + 275;
                }
                }
                return inNum;
                }





                share|improve this answer


























                  0














                  Here is the jsfiddle. You can type in the points and see the rewards value.



                  Your point system seems arbitrary so I changed to always start on 1 rather than sometime 0 and sometimes 1. If you don't like it, then you can change the function.



                  function points(inText) {
                  var inNum = (isNaN(Number(inText))) ? 0 : Math.floor(Number(inText));
                  inNum = Math.floor(inNum / 100000);
                  if (inNum < 5) {
                  inNum = inNum * 30;
                  } else {
                  if (inNum < 10) {
                  inNum = (inNum - 5) * 25 + 150;
                  } else {
                  inNum = (inNum - 10) * 20 + 275;
                  }
                  }
                  return inNum;
                  }





                  share|improve this answer
























                    0












                    0








                    0






                    Here is the jsfiddle. You can type in the points and see the rewards value.



                    Your point system seems arbitrary so I changed to always start on 1 rather than sometime 0 and sometimes 1. If you don't like it, then you can change the function.



                    function points(inText) {
                    var inNum = (isNaN(Number(inText))) ? 0 : Math.floor(Number(inText));
                    inNum = Math.floor(inNum / 100000);
                    if (inNum < 5) {
                    inNum = inNum * 30;
                    } else {
                    if (inNum < 10) {
                    inNum = (inNum - 5) * 25 + 150;
                    } else {
                    inNum = (inNum - 10) * 20 + 275;
                    }
                    }
                    return inNum;
                    }





                    share|improve this answer












                    Here is the jsfiddle. You can type in the points and see the rewards value.



                    Your point system seems arbitrary so I changed to always start on 1 rather than sometime 0 and sometimes 1. If you don't like it, then you can change the function.



                    function points(inText) {
                    var inNum = (isNaN(Number(inText))) ? 0 : Math.floor(Number(inText));
                    inNum = Math.floor(inNum / 100000);
                    if (inNum < 5) {
                    inNum = inNum * 30;
                    } else {
                    if (inNum < 10) {
                    inNum = (inNum - 5) * 25 + 150;
                    } else {
                    inNum = (inNum - 10) * 20 + 275;
                    }
                    }
                    return inNum;
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 6 '14 at 2:25









                    Chi Row

                    936617




                    936617























                        -1














                        I'm a little tired but I think this might work for any amountOfPoints:



                        var price = 30,
                        strPoints = amountOfPoints.toString(),
                        points = Number(amountOfPoints);

                        if (points >= 100000) {
                        var multiplier = Number(points.toString().substring(0, 5 - strPoints.length));
                        price += 20 * multiplier;
                        }


                        Basically just doing price = 30 + 20 * the_significant_digits_of_amountOfPoints where amountOfPoints is greater than 100,000. The substring part gives you 1 when there's 6 digits, 2 when there's 7, and so on.



                        This should work for the use case "It should increment by $20 for each 100,000". But your case statements assign values to price that don't follow this pattern. So you'd have to use this code after your points are large enough to surpass the conditions in those.






                        share|improve this answer




























                          -1














                          I'm a little tired but I think this might work for any amountOfPoints:



                          var price = 30,
                          strPoints = amountOfPoints.toString(),
                          points = Number(amountOfPoints);

                          if (points >= 100000) {
                          var multiplier = Number(points.toString().substring(0, 5 - strPoints.length));
                          price += 20 * multiplier;
                          }


                          Basically just doing price = 30 + 20 * the_significant_digits_of_amountOfPoints where amountOfPoints is greater than 100,000. The substring part gives you 1 when there's 6 digits, 2 when there's 7, and so on.



                          This should work for the use case "It should increment by $20 for each 100,000". But your case statements assign values to price that don't follow this pattern. So you'd have to use this code after your points are large enough to surpass the conditions in those.






                          share|improve this answer


























                            -1












                            -1








                            -1






                            I'm a little tired but I think this might work for any amountOfPoints:



                            var price = 30,
                            strPoints = amountOfPoints.toString(),
                            points = Number(amountOfPoints);

                            if (points >= 100000) {
                            var multiplier = Number(points.toString().substring(0, 5 - strPoints.length));
                            price += 20 * multiplier;
                            }


                            Basically just doing price = 30 + 20 * the_significant_digits_of_amountOfPoints where amountOfPoints is greater than 100,000. The substring part gives you 1 when there's 6 digits, 2 when there's 7, and so on.



                            This should work for the use case "It should increment by $20 for each 100,000". But your case statements assign values to price that don't follow this pattern. So you'd have to use this code after your points are large enough to surpass the conditions in those.






                            share|improve this answer














                            I'm a little tired but I think this might work for any amountOfPoints:



                            var price = 30,
                            strPoints = amountOfPoints.toString(),
                            points = Number(amountOfPoints);

                            if (points >= 100000) {
                            var multiplier = Number(points.toString().substring(0, 5 - strPoints.length));
                            price += 20 * multiplier;
                            }


                            Basically just doing price = 30 + 20 * the_significant_digits_of_amountOfPoints where amountOfPoints is greater than 100,000. The substring part gives you 1 when there's 6 digits, 2 when there's 7, and so on.



                            This should work for the use case "It should increment by $20 for each 100,000". But your case statements assign values to price that don't follow this pattern. So you'd have to use this code after your points are large enough to surpass the conditions in those.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Feb 6 '14 at 1:35

























                            answered Feb 6 '14 at 1:24









                            DiegoSalazar

                            10.8k22336




                            10.8k22336






























                                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%2f21591990%2fjavascript-math-calculation-case%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







                                Popular posts from this blog

                                How to pass form data using jquery Ajax to insert data in database?

                                National Museum of Racing and Hall of Fame

                                Guess what letter conforming each word