/    Sign up×
Community /Pin to ProfileBookmark

i dont understand this javascript function

Here is a javascript function i dont understand

<script>
function CheckDuplicates()
{
var values = new Array()
values[0] = document.form1.test.options[document.form1.test.selectedIndex].value
values[1] = document.form1.test2.options[document.form1.test2.selectedIndex].value
values[2] = document.form1.test3.options[document.form1.test3.selectedIndex].value

for (var i=0; i<values.length;i++)
if (values.join(“~”).indexOf(values[i]) != values.join(“~”).lastIndexOf(values[i]))
{alert(“duplicate found”);return;}
}
</script>

I am stuck exactly here
if (values.join(“~”).indexOf(values[i]) != values.join(“~”).lastIndexOf(values[i]))

How it works ?

let me understand in step by step.

(1)
i see , values.join(“~”) =====> whats does it do ? i see a special char “~” ..whats the role of that ?

(2) values[i]) != values.join(“~”).lastIndexOf(values[i])====>what the heck going on here ? this is very much complex lookin….plz explain this part.

someone please explain in a simpler way.

thanks

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERSep 17.2008 — Here is a javascript function i dont understand


<script>

function CheckDuplicates()

{

var values = new Array()

values[0] = document.form1.test.options[document.form1.test.selectedIndex].value

values[1] = document.form1.test2.options[document.form1.test2.selectedIndex].value

values[2] = document.form1.test3.options[document.form1.test3.selectedIndex].value

for (var i=0; i<values.length;i++)

if (values.join("~").indexOf(values[i]) != values.join("~").lastIndexOf(values[i]))

{alert("duplicate found");return;}

}

</script>

[/QUOTE]




Let's try this way.

Assuming two conditions:

Condition A: Contents of form1.test1 through form1.test3 contains 'A','B','C'

Condition B: Contents of form1.test1 through form1.test3 contains 'A','B','A'





I am stuck exactly here

if (values.join("~").indexOf(values[i]) != values.join("~").lastIndexOf(values[i]))





How it works ?



let me understand in step by step.

[/QUOTE]


In condition A: the action of 'values.join("~") would create a string from the 3 elements of the values array

It would appear as 'A~B~C';



In condition B: the action of 'values.join("~") would create a string from the 3 elements of the values array

It would appear as 'A~B~A';





(1)

i see , values.join("~") =====> whats does it do ? i see a special char "~" ..whats the role of that ?





(2) values[i]) != values.join("~").lastIndexOf(values[i])====>what the heck going on here ? this is very much complex lookin....plz explain this part.





someone please explain in a simpler way.



thanks
[/QUOTE]


'indexOf() compares the contents of the string within the () to the reference string 'A~B~C'

and returns the first position of the string that matches or -1 if no match is found

Each element is compared, in turn [0..2] to this string.



The first position of a match is returned for the left half comparison of the '!='

For condition A:

The returned values would be '0', '2','4' as it steps through the 'for' loop



For condition B:

The returned values would be '0', '2','0' as it steps through the 'for' loop



Then, the right half of the '!=' comparison is created much the same way, except the last position of a match is returned.



For conditon A this right half would return '0','2','4' and find that they would always equal the left half ot the '!=' comparison, so the comparison would always be false (the positions are always the same).



For condition B this right half would return '0','2','4' and find that they do not match (remember '0',2','0'). The comparison would then be true (the positions in the array for each values element checked are NOT the same, so the alert message is displayed.



Make sense now? ?
×

Success!

Help @cofactor 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 6.1,
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,
)...