0

How can I write a function instead of this Replace snippet and use it inside my checkAnswer method?

    methods: {

    checkAnswer: function () {

    this.quiz.userInputArray = this.quiz.userAnswerArray.replace('Ğ','g')
      .replace('Ü','u')
      .replace('Ş','s')
      .replace('I','i')
      .replace('ç','c');
    }

I want to be able to use it code like this:

this.quiz.userInputArray = this.quiz.userAnswerArray.replaceFunc()
maxshuty
  • 9,708
  • 13
  • 64
  • 77
Fereydoon
  • 71
  • 1
  • 9

1 Answers1

2

you can create another method called replaceFunc

methods: {
 replaceFunc(arr){
    return arr.replace('Ğ','g')
     .replace('Ü','u')
     .replace('Ş','s')
     .replace('I','i')
     .replace('ç','c');
 },

 checkAnswer(){
    this.quiz.userInputArray = this.replaceFunc(this.quiz.userAnswerArray);
 }
}

Otherwise, if you insist on calling replaceFunc like this this.quiz.userAnswerArray.replaceFunc(), then you can read about adding custom properties to Array.prototype which is considered a bad practice (adding custom functions into Array.prototype).

Helper function
  • 220
  • 1
  • 3