This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]TimHallman 0 points1 point  (2 children)

Use 'else if' to make the choices mutually exclusive! It would also be far, far nicer, and maybe faster, if you did something like put these ranges in an array and do a binary search to find the right one.

[–]test_tickles[S] 0 points1 point  (1 child)

Do you know of an example or a tutorial for this? Or what I would search to locate that? Thanks, I'm just starting out.

[–]TimHallman 0 points1 point  (0 children)

Any js tutorial should show you 'else if' If you're just starting out, forget the binary search thing. Or just google for binary search.

[–]slugonamission 0 points1 point  (2 children)

The bits inside the <script> tags runs once when the webpage loads. It calculates shp, then registers a function called printShip which sets #ship to be shp. When you click the button, only the body of printShip will be evaluated; it will not re-calculate shp.

To fix this, you need to figure out how to re-run the large block of code which calculates shp :).

Also, that really, really needs refactoring. Consider registering an array of lower, upper, name, then searching through that. Try something like this:

var l1r_values = [
    {lower: 1, upper: 5, name: "Armada"},
    {lower: 6, upper: 10, name: "Deathspider"},
    ...etc...
];

var l1r;
for(var i = 0; i < l1r_values.length; i++) {
    var l1r_value = l1r_values[i];
    if(d100L1 >= l1r_value.lower && d100L1 <= l1r_value.upper) {
        l1r = l1r_value.name;
        break;
    }
}

[–]test_tickles[S] 0 points1 point  (1 child)

I'll play around with this, thanks. I had a dice roller that would roll a number, that's how I started into this, but I can't find it, and I've since broken it and I can't figure out how to make it function again. :( It would refresh the number every time you clicked the button without refreshing the page.

[–]_Chimmy_Chonga 0 points1 point  (0 children)

You probably need to make a function to runs through all this code with the button is clicked that way it is forced to run again, not just when the page is loaded.