0

With the code below, looking at the 2nd paragraph in particular...if var str = "sting equality test..." + strA, why do the 2nd, 3rd lines etc not output that same output plus their own line?

EDIT sorry for not explaining properly - I am actually just wondering why this code (once I have cleaned it up) DOESN'T produce the first line (of the 2nd paragraph) repeated, plus whatever I state in the 2nd and 3rd lines etc. I don't need it to, it's just an exercise, I just don't understand. It seems as though it should

function init()
{
    var strA = "Javascript" === "JAVASCRIPT" ;
    var strB = "Javascript" === "Javascript" ;
    var flt = 7.5 === 7.5 ;
    var intA = 8 !== 8 ;
    var intB = 24 > 12 ;
    var intC = 24 < 12 ;
    var intD = 24 <= 24 ;

    var str = "String equality test: " + strA ;
    str += "<br>String equality test 2: " + strB ;
    str += "<br>Float equality test: " ; + strC ;
    str += "<br>Integer inequality test: " + intA ;
    str += "<br>Greater than test: " + intB ;
    str += "<br>Less than test: " + intC ;
    str += "<br>Less than/Equal to test: " + intD ;

    document.getElementById( "panel" ).innerHTML = str ;
}
document.addEventListener("DOMContentLoaded" , init , false) ;

So the output I get is as follows;

String equality test: false
String equality test 2: true
Float equality test: true
Integer inequality test: false
Greater than test: true
Less than test: false
Less than/Equal to test: true

This is obviously correct, I just don't understand how something like the following isn't outputted because surely I am adding each line to the var str, which is "String equakity test: false

 String equality test: false
String equality test: false String equality test 2: true
String equality test: false Float equality test: true
String equality test: false Integer inequality test: false
String equality test: false Greater than test: true
String equality test: false Less than test: false
String equality test: false Less than/Equal to test: true
4
  • Why have you got a mysterious floating ; in the third line? Commented Oct 6, 2013 at 14:32
  • ^^ because there is no strC ? Commented Oct 6, 2013 at 14:32
  • @Jeffman: was that a response to me, or the OP? DJC: if you'd looked at your Web Developer tools (F12 in most browsers) they would have told you this. Also, pressing the 'JS Hint' button, if you'd put together a JS Fiddle. Commented Oct 6, 2013 at 14:35
  • @David Both, I guess. To OP, the undefined variable might be a problem. To you, the seemingly stray ; might be a misguided attempt at short-circuiting the concatenation of a value that doesn't exist. Commented Oct 6, 2013 at 14:38

2 Answers 2

1

There is a ; on the third line in front of the +. Removing that should solve the problem. The variable strC which you try to add after that strange + doesn't exist.

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

5 Comments

thanks, have changed that now, but please see my edit, sorry for not explaining properly first time around
@DJC, you mean your code still doesn't output all lines after solving the problems I mentioned?
This is an exercise from a book and I am trying to understand why the var str, when used in the 2nd, 3rd line etc doesn't include the "string equality test" bit as isn't that what I have defined it as. The other lines look as though they should produce that, then the added values they contain?
Can you add your exact output and your expected output to the question?
sorry for delay, will edit question with expected and actual output. thanks forthe help, much appreciated
0

I just don't understand how something like the following isnt outputted because surely I am adding each line to the var str, which is "String equakity test: false"

No, the only time that str is equal to "String equality test: false" is immediately after the first line. Each line modifies the str variable by adding something to whatever it became after the line before. So after the first line:

var str = "String equality test: " + strA ;

...the variable str is now equal to

"String equality test: false"

Then after the second line:

str += "<br>String equality test 2: " + strB ;

...the variable str is now equal to

"String equality test: false<br>String equality test 2:"

The third line has an error, but assuming you remove the extra semicolon and fix the variable name:

str += "<br>Float equality test: " ; + strC ;
// should be
str += "<br>Float equality test: " + flt;

...then the variable str will be equal to

"String equality test: false<br>String equality test 2: true<br>Float equality test: true"

...and so forth.

You can see this clearly if you add a console.log(str); statement in between each line and open your browser's console before running the code. As shown here: http://jsfiddle.net/mfKy9/

1 Comment

thanks for that, very clear and helpful. i was just getting confused with the += , thinking it was keeping the line then adding the next, whereas like you say, it is actually replacing the previous value of var str. thanks!

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.