/    Sign up×
Community /Pin to ProfileBookmark

‘filter’ function modification

I modified the following code from:
[url]https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_filter[/url]

It works OK, but I would like to change it further.

I wish to somehow pass the GLOBAL variable ‘state’
as part of the ‘filter’ function so as to avoid global assignments.

How is the information passed in the ‘filter’ function? ?

[code]

<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″ />
<title> filter() test </title>
<body>

<pre>
Count ‘y’ &amp; ‘n’ &amp ‘-‘ characters
in this array [‘y’,’n’,’y’,’n’,’y’]
</pre>
<button onclick=”myFunction()”>Try it</button>
<pre id=”demo”></pre>

<script>
function doc(IDS) { return document.getElementById(IDS); } // reduce typing function

var consent = [‘y’,’n’,’y’,’n’,’y’],
state = ‘y’;
function checkConsent(YN) { return YN == state; }

function myFunction() {
state = ‘y’; doc(“demo”).innerHTML = ‘Y: ‘+ consent.filter(checkConsent).length+'<br>’;
state = ‘n’; doc(“demo”).innerHTML += ‘N: ‘+ consent.filter(checkConsent).length+'<br>’;
state = ‘-‘; doc(“demo”).innerHTML += ‘-: ‘+ consent.filter(checkConsent).length+'<br>’;
}
</script>
</body>
</html>

[/code]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJan 09.2018 — Unfortunately the parameters of the callback function cannot be modified. The only solution I'm aware of is this:
function myFilter(arr, state) {
function checkConsent(YN) {
return YN == state;
}
return arr.filter(checkConsent);
}
function myFunction() {
doc("demo").innerHTML = 'Y: ' + myFilter(consent, 'y').length + '&lt;br&gt;';
doc("demo").innerHTML += 'n: ' + myFilter(consent, 'n').length + '&lt;br&gt;';
doc("demo").innerHTML += '-: ' + myFilter(consent, '-').length + '&lt;br&gt;';
}
Unfortunately defining the callback in the global scope doesn't work:
function checkConsent(YN) {
return YN == state;
}
function myFilter(arr, state, callback) {
return arr.filter(callback);
}
function myFunction1() {
doc("demo").innerHTML = 'Y: ' + myFilter(consent, 'y', checkConsent).length + '&lt;br&gt;';
doc("demo").innerHTML += 'n: ' + myFilter(consent, 'n', checkConsent).length + '&lt;br&gt;';
doc("demo").innerHTML += '-: ' + myFilter(consent, '-', checkConsent).length + '&lt;br&gt;';
}
Copy linkTweet thisAlerts:
@SempervivumJan 09.2018 — PS: Researched further and figured out this:
function checkConsent(YN) {
return YN == this.state;
}
function myFilter(arr, st, callback) {
var par = { state: st };
return arr.filter(callback, par);
}
function myFunction1() {
doc("demo").innerHTML = 'Y: ' + myFilter(consent, 'y', checkConsent).length + '&lt;br&gt;';
doc("demo").innerHTML += 'n: ' + myFilter(consent, 'n', checkConsent).length + '&lt;br&gt;';
doc("demo").innerHTML += '-: ' + myFilter(consent, '-', checkConsent).length + '&lt;br&gt;';
}

Explanation:

[url]https://developer.mozilla.or
Copy linkTweet thisAlerts:
@SempervivumJan 09.2018 — PPS: This procedure can be easily applied to your initial code:
function checkConsent(YN) {
return YN == this.state;
}

<i> </i> function myFunction1() {
<i> </i> doc("demo").innerHTML = 'Y: ' + consent.filter(checkConsent, {state: 'y'}).length + '&lt;br&gt;';
<i> </i> doc("demo").innerHTML += 'n: ' + consent.filter(checkConsent, {state: 'n'}).length + '&lt;br&gt;';
<i> </i> doc("demo").innerHTML += '-: ' + consent.filter(checkConsent, {state: '-'}).length + '&lt;br&gt;';
<i> </i> }
Copy linkTweet thisAlerts:
@JMRKERauthorJan 09.2018 — PPS: This procedure can be easily applied to your initial code:
function checkConsent(YN) {
return YN == this.state;
}

<i> </i> function myFunction1() {
<i> </i> doc("demo").innerHTML = 'Y: ' + consent.filter(checkConsent, {state: 'y'}).length + '&lt;br&gt;';
<i> </i> doc("demo").innerHTML += 'n: ' + consent.filter(checkConsent, {state: 'n'}).length + '&lt;br&gt;';
<i> </i> doc("demo").innerHTML += '-: ' + consent.filter(checkConsent, {state: '-'}).length + '&lt;br&gt;';
<i> </i> }
[/QUOTE]


Thank you for the code and the reference.

Nice solution. ?
×

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,
)...