/    Sign up×
Community /Pin to ProfileBookmark

Testing action of a new(?) function

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>
[/code]

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[/url]

Can anyone tell me where my thinking is going awry? ?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERauthorFeb 13.2018 — Upon more detailed reading, I found the answer.



fromIndex is greater than or equal to the array length

If fromIndex is greater than or equal to the length of the array, false is returned. The array will not be searched.
[/quote]

Function is working correctly as advertised. (But I don't like the underlying logic)

I was interpreting the second parameter of the .includes function incorrectly.
Copy linkTweet thisAlerts:
@rootFeb 13.2018 — All the examples I have found only use one parameter in the includes() method.
Copy linkTweet thisAlerts:
@JMRKERauthorFeb 13.2018 — All the examples I have found only use one parameter in the includes() method.[/QUOTE]

Thanks. I've come to that conclusion as well even though there are some examples that use two parameters. That is what confused me as I was thinking that more than one value at a time could be tested for a true/false response.

I'm not sure of the difference between .indexOf and .includes

It appears that both work the same except for the response.

To me the .indexOf is more useful as it either returns the position (in the string OR array) of the found value or -1 if the value is not found.

The includes only returns true/false if the test value is found or not found either from the start of the array or a specified index location. I guess this might be used to determine if duplicate values are in an array by first locating the 1st value found position and using that in the includes to see if additional similar values are in the rest of the test array.
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...