-1

I have a string variable and I want to know if the multiplication symbol is in the string. This is what I did:

const str = "45*33"
const arr = str.split('')
if(arr.includes("*"){
console.log("Symbol found!")
}
else{
console.log("Error")
})


This does not work, is there a way I can make this work?

Joseph A
  • 12
  • 3
  • Does this answer your question? [How to check whether a string contains a substring in JavaScript?](https://stackoverflow.com/questions/1789945/how-to-check-whether-a-string-contains-a-substring-in-javascript) – enzo Jun 27 '21 at 20:41
  • Also you can check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf – Teoman shipahi Jun 27 '21 at 20:41

3 Answers3

1

You don't need to split at all; you can directly use String#includes.

const str = "45*33";
if (str.includes("*")) {
    console.log("Symbol found!")
} else {
    console.log("Error")
}

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

Use the console of your browser to check for errors. It is a simple syntax error at:

if(arr.includes("*"){

should be:

if(arr.includes("*")){
coldgas
  • 83
  • 4
0

Working perfectly now, it was a typo in the code that my eye just did not see. There is a reason why I had to split the string to an array. I could have just used the str.includes().

const str = "45*33"
const arr = str.split('')
if(arr.includes("*")){
console.log("Symbol found!")
}
else{
console.log("Error")
})

Thank you.

Joseph A
  • 12
  • 3