开发者

Creating an array of 20 numbers, printing each value as long as they aren't a duplicate, using the smallest array possible?

开发者 https://www.devze.com 2023-02-14 14:23 出处:网络
I need to print out a second array, that takes the numbers from the first array. The second array will only print the number once if it has a duplicate. The code I have now prints the last number of t

I need to print out a second array, that takes the numbers from the first array. The second array will only print the number once if it has a duplicate. The code I have now prints the last number of the first array 20 times.... I can't for the life of me figure out how to do this. I hate coming here with simple homework questions but the tu开发者_C百科tor is no help. If you can help me, please include some comments with what your doing, I want to learn this!

<html>
<head>
<title> HW 10.12 </title>
<script type="text/javascript">

var array=new Array(19);
var array2 =new Array();

for(var j=0; j< array.length; j++)
{
array[j]=Math.floor(10 + Math.random()*100);
}

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

for(var k=0; k < array.length; k++)
{ 
if (array2[k] != array[i])
{ 
array2[k] = array[i];
}}


document.writeln(array2)

</script>
</head>
<body>
</body>
</html>


I'm going to be super helpful today, just because it's Friday:

var array=new Array(19);
var array2 =new Array();

// Nothing changed here, just some formatting...
for(var j = 0; j < array.length; j++) {
  array[j]=Math.floor(10 + Math.random()*100);
}

// Declare a function variable. The function will determine
// whether a value is already contained in the array or not.
var hasValue = function(val,arr) {
  // Loop through the array "arr" (passed through as parameter) and
  // determine whether the value "val" (also passed through as parameter)
  // already exists in the array. If it does, return "true".
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] == val) 
      return true;
  }
  // If, after looping through all items in the array, the value isn't found,
  // return "false".
  return false;
};

// Loop through the original array.
for (var i = 0; i < array.length; i++) {
  // Call the "hasValue" function to determine whether the current item in the
  // original array already exists in the new array. If it doesn't, add it to
  // the new array.
  if (!hasValue(array[i],array2))
    array2.push(array[i]);
}

// Nothing changed here...
document.writeln(array2);


Change your javascript to as follows:

var array = new Array(19);
var array2 = new Array();

for (var j = 0; j < array.length; j++) {
    array[j] = Math.floor(10 + Math.random() * 100);
}

var isDuplicate = false;    
for (var i = 0; i < array.length; i++) {
    isDuplicate = false;
    for (var k = 0; k < array.length; k++) {
        if (array2[k] == array[i]) { //Check if this current value of array1 exists in array2
         //If yes then we should ignore this value
         // hence mark isDuplicate as true.
          isDuplicate = true; 
          break; //Break from loop as we don't need to check any further
        }

    }
    if(!isDuplicate ) //if isDuplicate is not true this value is unique. Push it to array2
       array2.push(array[i]); 
}

document.writeln(array2)
0

精彩评论

暂无评论...
验证码 换一张
取 消