1

I'm new to vue.js. I've tried this way to compare two strings, but it seems to fail in vue. How can I solve this issue?

I've tried this:

app.vue

@Component
export default class Game extends Vue{
  public word = ""
  public words: Array<string> = ["hello", "world"]

  public checkValue(): void{
    const firstWord = this.words.shift();
    if(firstWord === this.word){  // this condition is failing
      console.log("Success")
    }
  }
}

app.html

<md-input v-on:keyup.space="checkValue()" v-model="word"></md-input>

Why this condition is failing?

1
  • firstWord still array Commented Jun 21, 2020 at 10:41

2 Answers 2

3

Because you have an event keyup with space so word always have a space at end. Please trim before compare.

@Component
export default class Game extends Vue{
  public word = ""
  public words: Array<string> = ["hello", "world"]

  public checkValue(): void{
    const firstWord = this.words.shift();
    if(firstWord === this.word.trim()) {
      console.log("Success")
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

change the below block of code, it should solve your issue

  if (firstWord === this.word.trim()) {
      // to do
      console.log("Success");
    }

actually this.word always containing a space with your given word into input box. thats why it will never match. so you have to trim the given input word.

you can check the result

2 Comments

Hi, I've tested my string with console.log(typeof firstWord) and it shows string. How can I solve that?

Your Answer

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