I was trying out a JS method called .includes.
In experimenting with the method, I’ve come across a condition result that I don’t understand.
In the following script, note the areas marked with “???” in the output.
Those lines give a result that is opposite what I expected from the description
[code]
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″ />
<title> Includes and indexOf tests </title>
<style type=”text/css”>
b { color: red; }
</style>
</head>
<body>
<pre id=”output”></pre>
<script>
function doc(IDS) { return document.getElementById(IDS); }
var s = ”,
nlst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
alst = [‘0′,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9’];
s += ‘nNumeric array : using “indexOf”‘;
// one in many
if (nlst.indexOf(7)) { s += ‘n 7 is in [‘+nlst+’]’; }
// none in many
if (nlst.indexOf(-7)) { s += ‘n-7 is NOT in [‘+nlst+’]’; }
// includes from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
s += ‘nnNumeric array : using “includes”‘;
s += ‘n’+nlst.includes(-7)+’: [‘+nlst+’] includes number -7′; // false
s += ‘n ‘+nlst.includes(7)+’: [‘+nlst+’] includes number 7′; // true
s += ‘nn ‘+nlst.includes(7,1)+’: [‘+nlst+’] includes number 7,1′; // true
s += ‘n<b>??? ‘+nlst.includes(1,7)+’: [‘+nlst+’] includes number 1,7 ???</b>n’; // false ???
s += ‘n’+nlst.includes(7,-1)+’: [‘+nlst+’] includes number 7,-1′; // false
s += ‘n’+nlst.includes(1,-7)+’: [‘+nlst+’] includes number 1,-7′; // false
s += ‘n’+nlst.includes(-7,-1)+’: [‘+nlst+’] includes number -7,-1′; // false
// many in one
s += ‘nnString array : using “includes”‘;
s += ‘n’+alst.includes(7)+’: [“‘+alst.join(‘”,”‘)+'”] includes number -7’; // false
s += ‘n’+alst.includes(7)+’: [“‘+alst.join(‘”,”‘)+'”] includes number 7’; // false
s += ‘n ‘+alst.includes(“7”)+’: [“‘+alst.join(‘”,”‘)+'”] includes string “7”‘; // true
// many in many
s += ‘nn ‘+alst.includes(“7″,”1”)+’: [“‘+alst.join(‘”,”‘)+'”] includes string “7”,”1″‘; // true
s += ‘n<b>??? ‘+alst.includes(“1″,”7”)+’: [“‘+alst.join(‘”,”‘)+'”] includes string “1”,”7″ ???</b>n’; // false ???
s += ‘n’+alst.includes(“7″,”-1″)+’: [“‘+alst.join(‘”,”‘)+'”] includes string “7”,”-1″‘; // false
s += ‘n’+alst.includes(“-7″,”1”)+’: [“‘+alst.join(‘”,”‘)+'”] includes string “-7”,”1″‘; // false
s += ‘n’+alst.includes(“-7″,”-1″)+’: [“‘+alst.join(‘”,”‘)+'”] includes string “-7″,”-1″‘; // false
doc(‘output’).innerHTML = s.replace(/n/g,'<br>’);
</script>
</body>
</html>
In both cases I reversed the order of the parameters
but I did NOT expect a reversal of the expected “true” to “false” displays.
See reference:
[url]https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
Can anyone tell me where my thinking is going awry? ?