0

I have these 2 methods :

changeStartTime(event, id) {
    if (event.hour >= 10 && event.minute >= 10) {
      this.priceRules[id-1].startTime = event.hour+":"+event.minute;
    }
    else if (event.hour >= 10 && event.minute < 10) {
      this.priceRules[id-1].startTime = event.hour+":0"+event.minute;
    }
    else {
      this.priceRules[id-1].startTime = "0"+event.hour+":0"+event.minute;
    }
  }

  changeEndTime(event, id) {
    if (event.hour >= 10 && event.minute >= 10) {
      this.priceRules[id-1].endTime = event.hour+":"+event.minute;
    }
    else if (event.hour >= 10 && event.minute < 10) {
      this.priceRules[id-1].endTime = event.hour+":0"+event.minute;
    }
    else {
      this.priceRules[id-1].endTime = "0"+event.hour+":0"+event.minute;
    }
  }

As you can see, they are very very similar, the only thing that changes is startTime and endTime. It's a bad manner of a developer, so what I want is only one method with a third parameter ( maybe a string ? ) but i don't know how to do that. Thank you

1
  • this.priceRules[id-1]["typeTime"] ? did you try, you can this object notation. Commented May 22, 2019 at 10:23

2 Answers 2

3

Follow this syntax:

this.priceRules[id-1]['startTime']

Instead of this:

this.priceRules[id-1].startTime

i.e.

Here you can pass 'startTime' and 'endTime' as an argument of changeTime function

changeTime(event, id, key) {
    if (event.hour >= 10 && event.minute >= 10) {
      this.priceRules[id-1][key] = event.hour+":"+event.minute;
    }
    else if (event.hour >= 10 && event.minute < 10) {
      this.priceRules[id-1][key] = event.hour+":0"+event.minute;
    }
    else {
      this.priceRules[id-1][key] = "0"+event.hour+":0"+event.minute;
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

2

I think that this maybe will fix your problem, but not sure because i didn't see your priceRules structure.

changeTime(event, id, type) {
  if (event.hour >= 10 && event.minute >= 10) {
    this.priceRules[id-1][type] = event.hour+":"+event.minute;
  }
  else if (event.hour >= 10 && event.minute < 10) {
    this.priceRules[id-1][type] = event.hour+":0"+event.minute;
  }
  else {
    this.priceRules[id-1][type] = "0"+event.hour+":0"+event.minute;
  }
}

Where 'type' must be 'startTime' or 'endTime'.

Comments

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.