0

I am trying to prompt the user for a weight smaller than 126. Then my javascript is supposed to classify the weight into a category. I have used arrays but every time I loop it writes the 3rd array, superfly class. What can I do to make it function properly?

var wArray = ["fly", "superfly", "bantam", "superbantam", "feather"];
var weight = parseInt(prompt("What is your weight?"), 10);

while (weight > 126) {
    alert('Please enter a weight lighter than 126');
    weight = parseInt(prompt("What is your weight?"), 10);
}

recruit();

function recruit() {
    var weightClass = wArray[0];

    if (0 < weight && weight < 112) {
        weightClass = wArray[1];
    } else if (112 < weight && weight < 115) {
        weightClass = wArray[2];
    } else if (weight > 115 && weight < 118) {
        weightClass = wArray[3];
    } else if (weight > 118 && weight < 122) {
        weightClass = wArray[4];
    } else if (weight > 122 && weight < 126) {
        weightClass = wArray[5];
    }

    document.getElementById("weight").innerHTML = ('You are in ' + weightClass + ' class!');
}
3
  • You've got some gaps in your weight. Your if() structure makes it impossible for someone to be exactly 115 or 18 or 122 pounds. Commented Dec 6, 2011 at 19:19
  • should i use a float? to change to decimal? Commented Dec 6, 2011 at 19:27
  • No, it should be something like } else if (weight >= 115 && weight < 118) { so that 115 is (and the other numbers) are included as possible weights. (note the = sign in there). Commented Dec 6, 2011 at 19:44

2 Answers 2

2

Your first if condition is incorrect. You say if (112 < weight < 115). This first does 112 < weight, then takes the result of that and compares it to 115.

112 < weight evaluates to true or false; when used in numeric comparisons, true is 1 and false is 0. (Obviously) both 1 and 0 will always be less than 115, so this condition will always be true.


Also note that this script should be run onload of the page. This is because the div with the ID weight may not have loaded when the script executes and attempts to populate it. You can do this by saying:

<script type="text/javascript">
    var wArray = ["fly", "superfly", "bantam", "superbantam", "feather"];

    function calculateWeight() {
        var weight = parseInt(prompt("What is your weight?"), 10);

        while (weight > 126) {
            alert('Please enter a weight lighter than 126');
            weight = parseInt(prompt("What is your weight?"), 10);
        }

        recruit();
    }

    function recruit() {
        var weightClass = wArray[0];

        if (weight >= 112 && weight < 115) {
            weightClass = wArray[1];
        } else if (weight >= 115 && weight < 118) {
            weightClass = wArray[2];
        } else if (weight >= 118 && weight < 122) {
            weightClass = wArray[3];
        } else if (weight >= 122 && weight < 126) {
            weightClass = wArray[4];
        }

        document.getElementById("weight").innerHTML = ('You are in ' + weightClass + ' class!');

    }
</script>
<body onload="calculateWeight()">
    <!-- include body contents here -->
</body>
Sign up to request clarification or add additional context in comments.

1 Comment

Also note the >= comparison operators in the code above. Your original code effectively created one-pound gaps at the top of each weight class which would return wArray[0] for weightClass if entered by the user.
1

the line:

if (112 < weight < 115) {

Should be

if (112 < weight && weight < 115) {

4 Comments

To elaborate: No matter what number you use for weight, the expression 112 < weight < 115 will always be true.
yeah, there is still an error, it will not alert me of the final weight class
Do you get "fly" when you put in 126? You'll want to use <= instead of just <. But if one conditional is weight <= 122, the other should be weight > 122. Like what Marc B said above, you'll want to capture those instances where someone puts in the exact values. Which is what's happening with 126. (it gets past the while because weight (126) is not greater than 126. It's equal to.
at the moment, i dont get anything from it at all, but it used to keep putting me in superfly class with any number valid under 126

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.