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()
1
  • you could do this.quiz.userAnswerArray.map(ans=>ans.replace().replace()) Commented Dec 12, 2020 at 11:40

1 Answer 1

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).

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

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.