1

console.log doesn't display what I want it to show in my chrome browser dev console.

I have this code:

<!DOCTYPE html>

<html>

 <head>

  <meta charset="utf-8">

  <title></title>

 </head>

 <body>
  <script src="script.js">
       console.log(amazingCars);
       var amazingCars =["BMW","Lexus","Ford","Mercedes"]; 
       amazingCars.pop();
       console.log(amazingCars);
       amazingCars.sort();
       console.log(amazingCars);
       amazingCars.length;
       console.log(amazingCars.length);
   </script> 
 </body>

</html>

The directions for the assignment are the following:

Link the JavaScript page to the HTML page.
Create an array named amazingCars that contains four values:
    BMW
    Lexus
    Ford
    Mercedes
Remove the last value from the array
Next, sort the array in alphabetical order
Lastly, find the length of the array

I dont understand why it isn't displaying or the reason of this issue

3
  • 4
    remove the src from the script tag Commented Jul 22, 2019 at 16:59
  • 3
    the first console.log(amazingCars) is before you define var amazingCars = []. It's undefined at that position. Commented Jul 22, 2019 at 17:02
  • Also, amazingCars.length; does absolutely nothing. Anyway, either state a src that links to an external file, or put the JS code inside the tags. Not both. Commented Jul 22, 2019 at 17:06

3 Answers 3

1

If you are putting your JS code in html just add them between<script></script> tag.

You do not need to add src for script unless your JavaScript code is coming from a separate script file.

The src attribute specifies the URL of an external script file.

If you want to run the same JavaScript on several pages in a web site, you should create an external JavaScript file, instead of writing the same script over and over again. Save the script file with a .js extension, and then refer to it using the src attribute in the <script> tag

.

As per the instructions that you are following : "Link the JavaScript page to the HTML page."

  1. Create another file "index.js" in your project directory.
  2. Move your js code into the index.js file in your project directory and then use :

    <script src="index.js"></script> in your HTML.

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

2 Comments

Ya I kept it in the html when I should of been putting it in the .js, I got it now thank you
Happy to help :)
1

Firstly, make the <script src="index.js"></script> seperate from the code in the head, also unless if you have a seperate javascript file you dont need the src part at all, and secondly the first console.log outputs it before definition, creating an error. I hope this helped

2 Comments

I got thanks, the only issue is the HW wants me to link them which I don't understand how without ruining it again
@Ern Make a folder, name it whatever you want. Put your index.html in there and make a new file called index.js, put all of your javascript there than delete the code between the script elements. That way the script src references to a index.js file and the code should work how you want it to after you delete the first console.log
1

You have to change <script src="script.js"> to <script>.

Alternatively you can keep it this way and create a script.js file and copy paste all the code you have inside the <script> tag to the file. This file need to be in the same folder as your index.html file.

3 Comments

or create the file, and put the contents of the script tag in the file as per the assignment
I did that, it seemed to fix it though the homework still tells me to link them how do I do with without ruining it
type is not needed in html5 spec...unless it is not javascript

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.