user10985924
user10985924

Reputation:

Vue.Js comparing string failed

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?

Upvotes: 1

Views: 5061

Answers (2)

Thai Nguyen Hung
Thai Nguyen Hung

Reputation: 1212

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")
    }
  }
}

Upvotes: 3

Mahamudul Hasan
Mahamudul Hasan

Reputation: 2823

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

Upvotes: 0

Related Questions