Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is my code right, and is the book I am using wrong?

I am using John Pollocks "A beginners guide to javascript edition 3."

The lesson I am doing is 4-2 which can be found here:http://www.cs.tufts.edu/es/93IDI/refs/Pollock-3rd.pdf the page number for the PDF is 107(actual page number 83/84).

Correct me if I am wrong but the instructions say to print "Hi there!" to the screen, while giving an alert saying "regular text" after printing "This is strong text" to the screen.

So following the instructions this is what the js code named prjs4_2.js in the external file should be

function two_strings(text1,text2) {
    var added_text=text1+ " " +text2;
    return added_text;
}
function result() {
    var get_result=two_strings("Hi","there!");
    document.write(get_result);
}
var ff_result = two_strings("regular","text");
window.alert(ff_result);
result();

then here is the html code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>
    <body>
        <p><strong>"This is some strong text!"</strong></p>
        <script type="text/javascript" src="prjs4_2.js"></script>
    </body>
</html>

It took me a while to even get that code, because I was reading the result of what it should be, and then reading the text and thinking "this doesn't make sense" then I realized he has it backwards so I changed it to this.

function two_strings(text1,text2) {
    var added_text=text1+ " " +text2;
    return added_text;
}
function result() {
    var get_result=two_strings("Hi","there!");
    window.alert(get_result); 
}
var first_function_result = two_strings("regular","text");
document.write(first_function_result);
result();

then it came out the way he describes it should.

So my question is, is this book wrong, or did I just do it wrong somehow, or did I do it right by switching it around? I think I already know the answer but this whole question is just to make sure I am not going mad and that this book that has been published and is being used to teach people has incorrect information.

like image 779
Jonathan Hart Avatar asked Oct 01 '22 08:10

Jonathan Hart


1 Answers

You are completely correct. Steps 3-6 describe the code in your first snippet, while the description following the exercise expresses the result of your second snippet.

In publishing, errors are rather common - and the PDF is over 500 pages long. Unfortunately though, a quick Google search reveals no errata page, and according to an Amazon review it is either well-hidden or nonexistent. There does happen to be a fourth edition which may or may not have corrected this.

I salute you for being astute, reading carefully, and questioning when it doesn't make sense - it's a rather lost art these days. If you're doing this as part of a course, you might want to mention it to the professor or on a class discussion board. Otherwise, let's hope the next person finds this question and answer.

like image 53
lc. Avatar answered Oct 05 '22 11:10

lc.