0

Hi all. I have script that use two arrays. It pushes original array values to the newly created array. it dont pushes into new array duplicate values.
for Ex; the orginal array is var arr=new array will be var newArr=
the second three will not copied. but one disadvantage is that when it first meet the duplicated value after it it will not push another values into new array
i would like let it does like that
for ex . var arr=
var new arr=;
below is script

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd"
    >
<html>
<head>
    <title><!-- Insert your title here --></title>
    <script>
    var unique = function(origArr) {
    var newArr = [],

        origLen = origArr.length,
        found,
        x, y;

    for ( x = 0; x < origLen; x++ ) {
    //    found = undefined;
        for ( y = 0; y < newArr.length; y++ ) {
            if ( origArr[x] === newArr[y] ) {
              found = true;
              break;
            }
        }
        if ( !found) newArr.push( origArr[x] );
      
    }
        var az=[newArr,origArr];
  return newArr;
};

// Test it out
var myarray = ['jeffrey', 'allie', 'patty', 'damon', 'zach', 'jeffrey', 'allie','samir'];
var az = unique(myarray);
//document.write(az); // jeffrey, allie, patty, damon, zach
  
    
    
    
    </script>
</head>
<body>
    
    <input type="button" value="goster" onclick="document.write(az)">
</body>
</html>

thanks for attention

2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by azegurb
0

The problem you encounter right now is that you declare 'found' outside the for-loop scope. Once it is changed to 'true', it will always be 'true'. You need to initialize the value of 'found' before the checking loop. You did it, but it is incorrect.

Only modify your line 16 to 'found = false;' should do it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.