# I don't know why this isn't working HELP!

im trying to create a genetic network. Its kids difficult to explain but heres the jist of what the program should currently do. When i run the program it should give a list of creatures and give each creature a number between one and ten. After that it will sort them from highest to lowest then get rid of the 5 smaller numbers.

``````var running = true

var breeding = function()
{
var parent1 = Math.floor((Math.random() * 6));
var parent2 = Math.floor((Math.random() * 6));
var child = (medium[parenta1] + medium[parenta2]) / 2
medium.push(child);;
}

var f1 = function()
{
{
breeding()
}
return c1 = Math.floor((Math.random() * 10) + 1);
};

var f2 = function()
{
{
breeding()
}
return c2 = Math.floor((Math.random() * 10) + 1);
};

var f3 = function()
{
{
breeding()
}
return c3 = Math.floor((Math.random() * 10) + 1);
};

var f4 = function()
{
{
breeding()
}
return c4 = Math.floor((Math.random() * 10) + 1);
};

var f5 = function()
{
{
breeding()
}
return c5 = Math.floor((Math.random() * 10) + 1);
};

var f6 = function()
{
{
breeding()
}
return c6 = Math.floor((Math.random() * 10) + 1);
};

var f7 = function()
{
{
breeding()
}
return c7 = Math.floor((Math.random() * 10) + 1);
};

var f8 = function()
{
{
breeding()
}
return c8 = Math.floor((Math.random() * 10) + 1);
};

var f9 = function()
{
{
breeding()
}
return c9 = Math.floor((Math.random() * 10) + 1);
};

var f10 = function()
{
{
breeding()
}
return c10 = Math.floor((Math.random() * 10) + 1);
};

var medium = [f1(), f2(), f3(), f4(), f5(), f6(), f7(), f8(), f9(), f10()];

for(i = 0; i < medium.length; i++)
{
document.write("creature " + (i + 1) + ": " + medium[i] + "<br>");
};

document.write('<br>');

var murder = function()
{
var orderOfKilling = medium.sort(function(a, b){return b-a});
document.write(orderOfKilling);
};

murder();

for(i = 0; i <= 4; i++)
{
medium.pop(i);
};

document.write('<br>');
document.write('<br>');

document.write(medium);
``````

Okay, a few points to address here:

1. Debugging code

When asking for help with your code, providing annotations is invaluable for both yourself and others who want to read your code; the latter particularly because it gives us an indication of your intent verses your codeâ€™s output.

2. Refactoring code

You have 10 separate functions which are identical except for the return statement. If you have to change one of the functions, youâ€™d have to change all of them! A more efficient way to structure your code is to have a single function and pass in an argument.

More to the point, however, is that return statements shouldnâ€™t have declarations. The return statement returns the result of a function, so you can assign it where you called the function:

``````// Declare single f() function
var f = function() {
breeding();
}
return Math.floor((Math.random() * 10) + 1);
}

// Declare empty array
var medium = [];

// Add 10 elements to array
for (i = 0; i < 10; i++) {
medium.push(f()); // Element contains result of f()
}
``````

3. Your breeding() and f() functions

Iâ€™m not sure what the purpose of your `breeding()` function is. Is your intent to have the `f()` function return a value and save it in your `medium` array? What is `parenta1` and `parenta2`? You havenâ€™t declared either variable anywhere in your code. Why does your f() function check if `dead == true` if you havenâ€™t declared dead anywhere?

Again, until I know what your intent is for each line of code, I canâ€™t really help you much.

4. Order of declarations and execution

Declaring variables and functions should always come before any code execution. This helps prevent code being run before the function is defined, while also keeping things tidy and easier to read. I refer explicitly to your `murder()` function.

5. Using the sort() function

Using `sort()` on an array does not create a copy of the array, but alters the original, so thereâ€™s no need to declare the new variable `orderOfKilling`. You can check this by printing the results of both `orderOfKilling` and `medium` after calling `medium.sort()`.

6. Using the pop() function

If you want to remove the last five elements of an array, your condition should be `i < 5` (when starting from `i = 0`). Remember that the loop code will only run while the condition is `true`, like so:

0 < 5 - true, loop code runs
1 < 5 - true, loop code runs
2 < 5 - true, loop code runs
3 < 5 - true, loop code runs
4 < 5 - true, loop code runs
5 < 5 - false, loop code doesnâ€™t run

Also, when using `medium.pop()`, the last array element is removed, so thereâ€™s no need to use `medium.pop(i)`.

I can see some of the logic of what youâ€™re trying to achieve, but youâ€™re tripping yourself up on a few parts of your code with some minor misunderstandings. You also need to explain explicitly what each part of your code is doing (or at least, what it should be doing) so people will be able to properly understand where your code is going wrong.

1 Like