0

Hei, I'm working an app to simulate prices. I have a code like this.

  function max110(x) {
     if (x >= '1' && x <= '50') {
         var sum = 120 * x;
         hasil.value = 'Rp.' + parseFloat(sum * 1000);
     } else if (x >= '51' && x <= '100') {
         var sum = 115 * x;
         hasil.value = 'Rp.' + parseFloat(sum * 1000);
     } else if (x >= '101' && x <= '200') {
         var sum = 110 * x;
         hasil.value = 'Rp.' + parseFloat(sum * 1000);
     } else {
         hasil.value = 'error!';
     }
 }

 function max115(x) {
     if (x >= '1' && x <= '50') {
         var sum = 125 * x;
         hasil.value = 'Rp.' + parseFloat(sum * 1000);
     } else if (x >= '51' && x <= '100') {
         var sum = 120 * x;
         hasil.value = 'Rp.' + parseFloat(sum * 1000);
     } else if (x >= '101' && x <= '200') {
         var sum = 115 * x;
         hasil.value = 'Rp.' + parseFloat(sum * 1000);
     } else {
         hasil.value = 'error!';
     }
 }

And I still have some functions similar to that, it almost the same code I'm trying to make it simple, is it possible to make it in 1 function only?

3
  • is x a string or a number? and if a number, is it an integer? what is the expected range? Commented Oct 20, 2017 at 6:41
  • it's a string, should i need to parse it to integer? Commented Oct 20, 2017 at 6:41
  • i would use always numbers and it is unclear why you parse a numer. what do you want with it to achieve? Commented Oct 20, 2017 at 6:45

4 Answers 4

1

Try:

  function maxval(x,maxval) {
    if(x >= '1' && x <= '50'){
      var sum = (maxval+10)* x;
      hasil.value = 'Rp.'+parseFloat(sum*1000);
    }
     else if (x >= '51' && x <= '100'){
       var sum = (maxval+5)* x;
       hasil.value = 'Rp.'+parseFloat(sum*1000);
     }
     else if(x >= '101' && x <= '200'){
       var sum = (maxval)* x;
       hasil.value = 'Rp.'+parseFloat(sum*1000);
     }
     else{
       hasil.value = 'error!';
     }
  }

By the way i assumed that maxval increments by 5, Cant get you a better solution without getting more details about functionality.

Sign up to request clarification or add additional context in comments.

Comments

0

This would be my implementation. I don't agree with how you're handling your integers, but it's your coding style. I pass in an object of choice that has all of the values that I want. You don't need the logic, just the values. I hope nobody gets mad that I monkeypatch String. I'm assuming that your variable x is a string.

String.prototype.isBetween = function(lower, upper){
  int = parseInt(this)
  return int >= parseInt(lower) && int <= parseInt(upper)
}

max110 = {0: 120, 1: 115, 2: 110}
max115 = {0: 125, 1: 120, 2: 115}
function max(x, values) {
  let sum
  hasil.value = ''
  if (x.isBetween('1', '50')) {
    sum = values['0'] * x
  } else if (x.isBetween('51', '100')) {
    sum = values['1'] * x
  } else if (x.isBetween('101', '200')) {
    sum = values['2'] * x
  } else {
    hasil.value = 'error'
  }
  hasil.value = hasil.value ? 'error' : 'Rp.'+parseFloat(sum*1000);
}

6 Comments

Sorry I just get started javascript since two or three month ago I don't understand what it is LOL :)
@naovalluthfi which part?
Almost all of it, I don't understand this code 'String.prototype.isBetween'
Oh, that's me taking a little shortcut and patching the string object. I basically made my own method for a string. adding something to an Object prototype is how to add your own methods to objects in ES5 syntax.
@naovalluthfi Haha, well, the prototype style of JavaScript method creation is being masked by the class syntax in ES6. It's always good to know your fundamentals, but you can get by for a surprisingly long time without ever doing something like what I showed you in my code. My code works though, right?
|
0
function max(x, extra) {
  var sum_number = extra;
  if(x >= '1' && x <= '50'){
   sum_number += 120;
  }
  else if (x >= '51' && x <= '100'){
   sum_number += 115;
  }
  else if(x >= '101' && x <= '200'){
   sum_number += 110;
  }

  if(x < 1 && x > 200){
    hasil.value = 'error!';
  } else {
    hasil.value = 'Rp.'+parseFloat((sum_number) * x *1000);
  }
}

parameter extra can be 0 or 5 for function max110 or max115

Comments

0

Basically, you have two function which works the same way and returns the same with different values.

  • The different values yould be stored in an array and you could use a single function for getting the index and then take the needed value out of the array with that index.

  • So you need a better organisation of types of the variables, which if uses as number, it should be number and also for comparison, then it should be a number on both sides of the condition.

  • Use a pure function, which does not alter a state of something, which is not given into the function.

  • Use a check in the function for unwanted values and exit early with a first check at the lower border, in your case, it is zero and below, return -1, because that is not an index of an array (and it is usually used to denote, that no index is found, like with Array#indexOf).

  • Then take the upper border for a check end exit early with a index value, no need for continuing else if structures.

  • At the end return as well -1 for not found index.

Together:

function getValue(x, maxArray) {
    var index = getIndex(x);
    if (index in maxArray) {
        return 'Rp.' + maxArray[index] * x * 1000;
    }
    return 'error!';
}

function getIndex(x) {
    if (!x || x < 0) {
        return -1;
    }
    if (x <= 50) {
        return 0;
    }
    if (x <= 100) {
        return 1;
    }
    if (x <= 200) {
        return 2;
    }
    return -1;
}

var max110 = [120, 115, 110],
    max115 = [125, 120, 115];

console.log(getValue(-1, max110));
console.log(getValue(10, max110));
console.log(getValue(10, max115));

10 Comments

I think I understand how it works, but I don't understand why -1?
it's just a definiton, because that value is not an index of an array. you could have a look here: Array#indexOf
oh I know I know sorry, I just realized that when I read it again
But, console.log('Rp.' + max110[index] * x * 1000); can the max110[index] change automatically? Do you know what i mean?
you could move the complete logic into another function.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.