Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2010
    Location
    myeik
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Javascript Unit Testing with function parameter

    Code:
    function calculatePrice(base_cost, base_dist, additional_cost, additional_dist, total_dist) {
    
      if (total_dist > base_dist) {
    
    
    
      } else {
         return base_cost;
      }
    
    
    }
    
    function unitTest() {
      var cost;
    
      cost = calculatePrice(10,20,5,5,15);
      if (cost != 10) console.log ("FAILED 1");
    
      cost = calculatePrice(10,10,5,5,15);
      if (cost != 15) console.log ("FAILED 2");
    
      cost = calculatePrice(20,10,5,1,15);
      if (cost != 45) console.log ("FAILED 3");
    
      cost = calculatePrice(20,10,5,1,1);
      if (cost != 20) console.log ("FAILED 4");
    
      cost = calculatePrice(10,10,5,1,14);
      if (cost != 30) console.log ("FAILED 5");
    
      cost = calculatePrice(10,10,5,2,14);
      if (cost != 20) console.log ("FAILED 6");
    
      console.log('Test complete');
    
    }
    I want to calculate the function value of unitTest for passing.I pass only 1 and need to write some pass code in the calculatePrice function. Thanks a lot

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    It's not exactly clear (to me, anyway) what you want here. What do you want to happen if (total_dist > base_dist)?

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,373
    Thanks
    11
    Thanked 591 Times in 572 Posts
    what does "calculate the function value" mean?
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #4
    New Coder
    Join Date
    Aug 2010
    Location
    myeik
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 5 Posts
    Now I got the answer.All Test are Pass using calculatePrice Function

    function calculatePrice(base_cost, base_dist, additional_cost, additional_dist, total_dist) {
    if (total_dist > base_dist) {
    var dist_diff = total_dist - base_dist;
    var time = Math.ceil(dist_diff / additional_dist);
    return (time * additional_cost) + base_cost;
    }else {
    return base_cost;
    }
    }

    function calculate_pickup_price() {
    var cost;

    cost = calculatePrice(10,20,5,5,15);
    if (cost != 10) console.log ("FAILED 1");

    cost = calculatePrice(10,10,5,4,15);
    if (cost != 20) console.log ("FAILED 2");

    cost = calculatePrice(20,10,5,1,15);
    if (cost != 45) console.log ("FAILED 3");

    cost = calculatePrice(20,10,5,1,1);
    if (cost != 20) console.log ("FAILED 4");

    cost = calculatePrice(10,10,5,1,14);
    if (cost != 30) console.log ("FAILED 5");

    cost = calculatePrice(10,10,5,2,14);
    if (cost != 20) console.log ("FAILED 6");

    cost = calculatePrice(10,10,5,1,19);
    if (cost != 55) console.log ("FAILED 7");

    console.log('Test complete');

    }


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •