-3

I have a problem with my javascript code.

numbercars.js

var cars = {
chan1:'',
number:'2';}

index.html

<html>
    <head>
    <script type="text/javascript" src="numbercars.js"></script>
    <script>
    var numbercars = cars.number;

    if (numbercars =<9){
    //My action
    }
    else{
    //My second action
    } 
    </script>
    </head>
    <body>
       HTML CONTENT
    </body>
</html>

But this statement didn't work, all time, it's else.

Can you, if you can, tell me where is my error, i didn't understand.

Thank you

Orry
  • 643
  • 6
  • 19
Argardor
  • 69
  • 1
  • 1
  • 11
  • first file is numbercar.js and second numbercars.js is that correct? try change the cars name? – Orry Feb 10 '18 at 18:23
  • 4
    Possible duplicate of [Can I access variables from another file?](https://stackoverflow.com/questions/3244361/can-i-access-variables-from-another-file) – jrtapsell Feb 10 '18 at 18:24
  • @OrryVandermeulen first file numbercars.js who I declare the number of Cars. The second file is index.html who I need to make a statement from the number of cars. Exemple : if number >2, I can do an action, if <2, I can't do nothing – Argardor Feb 10 '18 at 18:28
  • What happens if you `console.log(cars)` in your second `script` tag? It seems to me that you are not loading the script properly. Are the files in the same directory? – nicodp Feb 10 '18 at 18:47

2 Answers2

1

You have two issues

A semi-colon in your cars object. Remove it.

var cars = {
    chan1:'',
    number:'2';
}
              ^

You're using an invalid combination of operators to compare numbers. Change to numbercars <= 9

if (numbercars =< 9){
               ^
Ele
  • 31,191
  • 6
  • 31
  • 67
0

Remove the semi-colon and quotation marks for integers.

var cars = {
  chan1:'',
  number: 2
}
Orry
  • 643
  • 6
  • 19
Ivo
  • 81
  • 1
  • 11