1

How can I take this code

<script>
var arr = [<%= myArray %>+<%= my2Array %>]; 
var sorted_arr = arr.sort(); 
var results = []; 
for (var i = 0; i < arr.length - 1; i += 1) { 
        if (sorted_arr[i + 1] == sorted_arr[i]) { 
                results.push(sorted_arr[i]); 
        } 
} 
 document.write(results +"<br />");
</script>

and convert it to <% the above script %> and keep the functionality?

Why does the script work in the script tag but not when I place it in <% %>

    <%
var arr = [myArray+my2Array]; 
var sorted_arr = arr.sort(); 
var results = []; 
for (var i = 0; i < arr.length - 1; i += 1) { 
        if (sorted_arr[i + 1] == sorted_arr[i]) { 
                results.push(sorted_arr[i]); 
        } 
} 
%>
<%= results %>

If I use it like this the results are returned as empty

I suspect the reason why it is not working is because the results value is not getting populates... In the script version it gets the time to populate and loop

5
  • Server side scripts are not written in javascript. What's the language you are using to write server scripts? Commented Feb 13, 2010 at 8:45
  • I am using asp and javascript Commented Feb 13, 2010 at 8:48
  • What do myArray and my2Array contain? - are they JScript arrays? I don't think you can just add them Commented Feb 13, 2010 at 8:53
  • It is text values - myArray and my2Array = 'this','is','a','test' Commented Feb 13, 2010 at 8:56
  • If those are ASP arrays, simply outputting them into the page won't work the way you expect. You'll need to have some ASP code loop over each array and echo the variables - so that when the page is sent to the client, it parses as valid JavaScript. Commented Feb 13, 2010 at 9:06

1 Answer 1

1

If you want to run your code server side you need to change the line

var arr = [myArray+my2Array]; 

to

var arr = (myArray+','+my2Array).split(',');
Sign up to request clarification or add additional context in comments.

1 Comment

var arr = (myArray+my2Array).split(','); var sorted_arr = arr.sort(); var results = []; for (var i = 0; i < arr.length - 1; i += 1) { if (sorted_arr[i + 1] == sorted_arr[i]) { results.push(sorted_arr[i]); } } Works 100% thanks meouw!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.