/    Sign up×
Community /Pin to ProfileBookmark

How to make a function / method to find other words in array?

Hi. I have a problem doing this task. I just learned it and don’t understand. Below is the case.

Make a function / method that accepts parameters in the form of an array whose contents are strings. The task of the function is to look for words that have the same alphabet / character as other words in the array. For example “paint” with “act”, “code” with “deco”. If none is found then the output is “Not found!”. Make a parameter check if the given parameter is not an array!

Input: findSame([“cat”, “listen”, “code”, “act”, “silent”, “tac”])
Output: cat, act, tac
listen, silent

Input: findSame([“try”, “fire”, “dark”])
Output: “Not Found!”

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERNov 02.2019 — Consider using the Array.filter() built-in JS function.
Copy linkTweet thisAlerts:
@cootheadNov 02.2019 — Hi novani,

and a warm welcome to these forums. ;)

Here is an example, that uses **JMRKER**'s suggestion...

``<i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;html lang="en"&gt;
&lt;head&gt;

&lt;meta charset="utf-8"&gt;
&lt;meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"&gt;

&lt;title&gt;Untitled document&lt;/title&gt;

&lt;!--&lt;link rel="stylesheet" href="screen.css" media="screen"&gt;--&gt;

&lt;style media="screen"&gt;
body {
background-color: #f9f9f9;
font: normal 1em / 1.5em BlinkMacSystemFont, -apple-system, 'Segoe UI', roboto, helvetica, arial, sans-serif;
}
#result {
display: inline-block;
padding: 1em;
border: 1px solid #999;
background-color: #fff;
}

#result span {
font-weight: bold;
}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;div id="result"&gt;&lt;/div&gt;

&lt;script&gt;
(function( d ) {
'use strict';
var vowel = ['a', 'e', 'i', 'o', 'u'],
words = [' cat', ' listen', ' code', ' act', ' silent', ' tac'],
c;
for ( c = 0; c &lt; vowel.length; c ++ ) {
const test = words.filter( word =&gt; word.match( vowel[c] ) );

if ( test.length &gt;= 2 ) {
d.getElementById( 'result' ).innerHTML +=
'The "&lt;span&gt;' + vowel[c]+ '&lt;/span&gt;" matches are - &lt;span&gt;' + test +'&lt;/span&gt;.&lt;br&gt;';
}
}
}( document ));
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;<i>
</i>
`</CODE>

Unfortunately, IE11 does not like it.<br/>
If you want IE11 support then try this more involved example...

<CODE>
`<i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;html lang="en"&gt;
&lt;head&gt;

&lt;meta charset="utf-8"&gt;
&lt;meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"&gt;

&lt;title&gt;Untitled document&lt;/title&gt;

&lt;!--&lt;link rel="stylesheet" href="screen.css" media="screen"&gt;--&gt;

&lt;style media="screen"&gt;
body {
background-color: #f9f9f9;
font: normal 1em / 1.5em BlinkMacSystemFont, -apple-system, 'Segoe UI', roboto, helvetica, arial, sans-serif;
}
#result {
display: inline-block;
padding: 1em;
border: 1px solid #999;
background-color: #fff;
}
#result span {
font-weight: bold;
}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;div id="result"&gt;&lt;/div&gt;

&lt;script&gt;
(function( d ) {
'use strict';

var vowel = ['a', 'e', 'i', 'o', 'u'],
ary = [' cat', ' listen', ' code', ' act', ' silent', ' tac'],
ary2 = [ [], [], [], [], [] ],
c, k;

for ( c = 0; c &lt; vowel.length; c ++ ) {
for ( k = 0; k &lt; ary.length; k ++ ) {
if ( ary[ k ].match( vowel[ c ] ) ) {
ary2[ c ].push( ary[ k ] );
}
}
if ( ary2[c].length &gt;= 2 ) {
d.getElementById( 'result' ).innerHTML +=
'The "&lt;span&gt;' + vowel[c]+ '&lt;/span&gt;" matches are - &lt;span&gt;' + ary2[c] +'&lt;/span&gt;.&lt;br&gt;';
}
}
}( document ));
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;<i>
</i>
``


_coothead_
Copy linkTweet thisAlerts:
@SempervivumNov 02.2019 — @novani#1610558 My version, presumed I understood the task correctly:
&lt;script&gt;
var words = ['cat', 'listen', 'code', 'act', 'silent', 'tac'];

<i> </i> // First we sort the letters, so that we can easily compare
<i> </i> // them later on: cat --&gt; act tac --&gt; act
<i> </i> var sortedWords = words.map(function (item) {
<i> </i> return item.split('').sort().join('');
<i> </i> });

<i> </i> var duplicates = [], // words that have the same set of letters
<i> </i> uniques = [], // words with a set of letters that appears only once
<i> </i> refs = []; // here we note which words have already been checked

<i> </i> // we check all sorted words:
<i> </i> for (var i = 0; i &lt; sortedWords.length - 1; i++) {
<i> </i> // current word is the reference we compare with
<i> </i> var ref = sortedWords[i];
<i> </i> // check only if it's not done yet
<i> </i> if (refs.indexOf(ref) == -1) {
<i> </i> // note current word
<i> </i> refs.push(ref);
<i> </i> // enter current word into temporary array
<i> </i> var intResult = [words[i]];
<i> </i> // we check every word after the current one
<i> </i> // if it is equal
<i> </i> for (var j = i + 1; j &lt; sortedWords.length; j++) {
<i> </i> if (sortedWords[j] == ref) {
<i> </i> // current word is equal
<i> </i> // append it to temp. array
<i> </i> intResult.push(words[j]);
<i> </i> }
<i> </i> }
<i> </i> // we check the temp. array:
<i> </i> // if there is only one entry
<i> </i> if (intResult.length &gt; 1) {
<i> </i> // there is more than one entry, the sorted word
<i> </i> // or set of letters occured multiplely,
<i> </i> // we append the original word
<i> </i> // to the corresponding result array
<i> </i> duplicates.push(intResult);
<i> </i> } else {
<i> </i> // there is only one entry, the sorted word
<i> </i> // or set of letters occured only once,
<i> </i> // we append the original word
<i> </i> // to the correspondige result array
<i> </i> uniques.push(words[i]);
<i> </i> }
<i> </i> }
<i> </i> }
<i> </i> console.log(duplicates);
<i> </i> console.log(uniques);
<i> </i>&lt;/script&gt;
×

Success!

Help @novani 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 5.26,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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