+ 9
Why does this code work with integers but not with strings?
https://code.sololearn.com/WxvI8a32AXFA/?ref=app Can someone help me fix it? Thank you.
5 ответов
+ 5
💧Alex Tusinean🔥, JS-in doesn't work like Python-in: It asks for the key in key-value pairs. (Or maybe, JS arrays react to in more like Python's dicts react to in.)
In a simple array, your 'keys' are digits (indexes) which are accidentally equal to the values, so it works, but in the other function, in still asks for the indexes.
If you want something like Python's in, something that asks for the values, take the method includes!
if (hi.includes(
document.getElementById(
"inp2").value)
)
+ 8
// try this:
// I tried it and it worked
if (~hi.indexOf(document.getElementById("inp2").value) ){
alert("work")
}
}
+ 8
Because you must specify index in object, but not the value of that index.
Not literal of array, but constructor Array. But index instead of value
let foo = new Array("one", "two");
0 in foo // true
"length" in foo // true (Array has this property)
"one" in foo // false
+ 2
I made the same mistake. 😑😅