0

I am fooling around with this function that says if you leave this text box blank than print the error code. if its not blank that print one of the text strings. The error code is working but I cannot get it to print any of the text strings. I think the problem is somewhere in returning the random selection to the html doc, I am not sure how to syntax this. When you click the button it executes the function

function button() {
    var a = "Punf";
    var b = "Relcken";
    var c = "Checks in the mail";
    var d = "Thae said";
    var e = "Drorself";
    var f = "Thertions";
    var g = "Don stupid";
    var err = "You think you are smarter than this website? Your not!"

    if (document.getElementById('ask').value == "") {
        document.getElementById('answer').innerHTML = err;
    }

    if (document.getElementById('ask').value == !"") {
        var qoute = (a, b, c, d, e, f, g);
        var maxQuote = quote.length;
        var randQuote = Math.floor(Math.random() * maxQuote);
        return quote(randQuote);
        document.getElementById('answer').innerHTML = a;
    }
}

I have got it down to here but I am still having issues and I am not sure why, Thanks for pointing out my rookie mistakes I need to take a step back and look at it with fresh eyes more.

if (document.getElementById('ask').value == ""){

document.getElementById('answer').innerHTML = err;

}
        var quote = [a,b,c,d,e,f,g];
        var maxQuote = quote.length;
        var randQuote = Math.floor(Math.random()*maxQuote);
        document.getElementById('answer').value = quote[randQuote];



    }
6
  • You do realize that because you have a return statement the innerHTML of "answer" is never going to get set? Commented Nov 3, 2011 at 21:13
  • You've spelled your var quote and qoute... Commented Nov 3, 2011 at 21:13
  • Spelling mistake in variable declaration. Also, use != "" not == !"" Commented Nov 3, 2011 at 21:15
  • Contest: I spot five major problems with this code. How many do you see? Commented Nov 3, 2011 at 21:16
  • @PeterOlson: 1) naming convention, 2) too many, unnecessary variables, 3) incorrect comparison, 4) incorrect array literal, 5) some parts of the code will never be executed, 6) this not executed part makes something dummy and does not need any calculations, 7) the button() function not always return value. Anything else? Commented Nov 3, 2011 at 21:29

5 Answers 5

2

What is == !''?

if (something equals not-a-blank-string)

?

What's the "not" of a blank string?

That should be !== '' instead.

Sign up to request clarification or add additional context in comments.

5 Comments

You are wrong - == !'' is not the same as !==''. The first is actually the same as == true and I believe you understand the difference between == true and !== ''?
No, I don't. What's the inverse of an empty string? A full string? Full of what? Nulls? 0s? random characters? Killer African bees? It's a syntactically horrible construct.
Horrible or not, this is sometimes used (in more meaningful situations, though). Have you seen eg. !!a (where a is some integer)? You would probably say " it is opposite of the opposite of some integer, it is horrible construct ", but for some it is just conversion from integer to boolean.
I was trying to state if the value is not null, does it not work this way?
@EdwardNickAJubrey If you want to compare something to null, use the constructs "a == null" or "a != null". '' is not the same as null: it signifies an empty (zero-length) string.
1

change

document.getElementById('ask').value == !""

to

document.getElementById('ask').value != ""

Comments

1

Point 1: array syntax is with [] and not ()

var qoute = [a,b,c,d,e,f,g];
...
quote[randQuote];

Point 2: return will prevent the execution of the last line

    return quote(randQuote);
    document.getElementById('answer').innerHTML = a; // never executed.

Comments

0

You have made a typo.

Change line:

var qoute = (a,b,c,d,e,f,g);

into the following:

var quote = [a,b,c,d,e,f,g];

and this line:

return quote(randQuote);

into this line:

return quote[randQuote];

and tell me if it fixed the problem.

Comments

0
var qoute = (a,b,c,d,e,f,g);

should be:

var qoute = [a,b,c,d,e,f,g];

basically:

var qoute = (a,b,c,d,e,f,g);

becomes:

var qoute = g; // commma operator

This:

if (document.getElementById('ask').value == !""){

should just be:

if (document.getElementById('ask').value) { // value has something, is not null or "" <-- empty

Comments

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.