Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,159 Points

JavaScript Assert calls both alerts

I have an assignment to create an assert function to check numeric values for a simple calculator. What I've written is syntactically valid but calls both alerts, not just the one that proclaims valid or not.

Been fooling with the braces for about 2 hours. What am I missing? I want valid values to show a valid alert (or preferably nothing but I'm not that good) and invalid ones (non-numeric or less than 1) to trigger the invalid warning.

Thanks.

    function assert(value) {
         var result = "Epic Fail";
         var description = "Invalid Value";
        if ((value != "number") ||  (value <= 0)    )   {





  alert(result + ' - ' +  description);
}
alert("pass");
    };

8 Answers

you not want to use "else"? after the if...

if ((value != "number") || (value < 0) ) { alert(result + ' - ' + description); } else alert("pass"); };

hmm. this is a real mindbender. everything i have tried just drops me out of the function. i tried a loop

// Get a reference to the form value:  
var length = document.getElementById('length').value;

var width = document.getElementById('width').value;

var height = document.getElementById('height').value;

const index = [length, width, height]

for (let i = 0; i < index.length; i += 1) {

assert.ok(index[i] != "number", "sorry " + index[i] + " is not a valid input"); assert.ok(index[i] < 0, "sorry " + index[i] + " must be a positive number" ); }

like that.. i pulled the vars out so i could access them from outside.. i did change the names but it didnt seem to cause any problems for the testing so i left them as you had written them.. this still kicks me out of the function for creating undefined things. i would love to know how this works out.

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,159 Points

It's still doing it...thanks. I'll keep trying. I may post the html later on today.

yeah if i can see the whole thing i might be able to recreate it

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,159 Points

Here's the latest version...I am going to work on another project for a little while. HTML follows JS

function assert(x) {
         var result = "Epic Fail";;
         var description = "Invalid Value";;
        if ((x != "number") ||  (x <= 0)    )   {

            alert(result + " , " + description);


        } else {

            //alert("pass");
    };



function calculate() {
    'use strict';

    // For storing the volume:
    var length;
    var width;
    var height;
    var volume;
    assert(length);
    assert(width);
    assert(height); 
    // Get a reference to the form value:
    var length = document.getElementById('length').value;

    var width = document.getElementById('width').value;

    var height = document.getElementById('height').value;




    volume = (length * width * height);


        // Format the volume:
    volume = volume.toFixed(2);

    // Display the volume:
    document.getElementById('volume').value = volume;

    // Return false to prevent submission:
    return false;

} // End of calculate() function.

// Function called when the window has been loaded.
// Function needs to add an event listener to the form.
function init() {
    'use strict';
    document.getElementById('theForm').onsubmit = calculate;
} // End of init() function.
window.onload = init;
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Nancy Melucci - Volume of a Box Calculator</title>
    <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link rel="stylesheet" href="css/styles.css">
</head>
<body>
    <!-- sphere.html -->
    <form action="" method="post" id="theForm">
        <fieldset>
            <p>Use this form to calculate the volume of a box.</p>
            <div><label for="length">Length</label><input type="text" name="length" id="length" required></div>
            <div><label for="width">Width</label><input type="text" name="width" id="width"></div>
            <div><label for="height">Height</label><input type="text" name="height" id="height"></div>      
            <div><label for="volume">Volume</label><input type="text" name="volume" id="volume"></div>  
            <div><input type="submit" value="Calculate" id="submit"></div>
        </fieldset>
    </form>
    <script src="js/box.js"></script>
</body>
</html>

thanks, im out tonight but i will look at this when i get home

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,159 Points

I appreciate your taking the time whenever you can. Thanks again.

also, whats the techuniversity stuff like? im a little hazy on what it actually is?

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,159 Points

You mean the techuniversity stuff here at Treehouse? I am asking here related to community college classes I am taking. I hope to work on the techdegree more this summer. NJM

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,159 Points

I had to put it aside to work on a bigger project. As I am looking forward to having more time in the summer to work on Treehouse and outside projects (I am a college prof and this is the worst time of the term for grading and things that compete for my attention, I can't dwell on this too long. I would like to make it work, so I will return to your code soon and try it out.)

Additionally, I have a bigger and slightly more amusing project in JS. I am posting about it elsewhere. You may see it soon. Thanks.

haha, well i will try and keep this on my radar. if i come across something that might help me solve it i will post it here!