Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't my JavaScript link work when called from HTML?

I'm pretty new to coding, and I'm trying to complete Codecademy's Javascript course. I've learned a little bit about HTML/CSS and I'm almost done with JavaScript. I've researched people having similar problems, but those solutions typically involve JQuery, which I haven't learned.

Here is my HTML (index.html):

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="main.js"></script>
  </head>
  <body>
  </body>
</html>

Here is the beginning of my JavaScript:

alert();

// Acquire character's name and check to make sure it's a string
var charName = prompt("NASA Receptionist: 'Welcome to Mission Control. 
May I have your last name, please?'");

var nameCheck = function(charName) {
    while (typeof charName === "number") {
        charName = prompt("NASA Receptionist: 'Surely, your name is not 
a number... Please, may I have your last name?'");
    }
};

nameCheck(charName);

NOTE: index.html is in the same folder as main.js

When I open the index.html, nothing happens, not even the opening alert(). Am I missing something?

like image 672
Goeff Avatar asked Aug 23 '26 13:08

Goeff


1 Answers

You have error in your script as you cannot make javascript statements in multiple lines without using escaping slash .

I was getting this error :

SyntaxError: unterminated string literal

var charName = prompt("NASA Receptionist: 'Welcome to Mission Control.

Here is the modified code :

    alert();

    // Acquire character's name and check to make sure it's a string
    //The \r\n\ will format the string in prompt and make it appear in new line
    var charName = prompt("NASA Receptionist: 'Welcome to Mission Control. \
                        \r\n\May I have your last name, please?'");

    var nameCheck = function(charName) {
        while (typeof charName === "number") {
            charName = prompt("NASA Receptionist: 'Surely, your name is not \
                                \r\n\a number... Please, may I have your last name?'");
        }
    };

    nameCheck(charName);
like image 121
shivgre Avatar answered Aug 25 '26 04:08

shivgre