I am a self learner, I am having issues running my test files on Node.js. I am using the following command: "node ". I don't know what it means by document not being defined. In this case I am typing node test.js. Any help would be nice. I am faced with the the following errors:
PS C:\Users\buddys\Desktop\Projects> node test.js
C:\Users\buddys\Desktop\Projects\test.js:8
document.write("Metri loves you too baby!");
^
ReferenceError: document is not defined
at Object.<anonymous> (C:\Users\buddys\Desktop\Projects\test.js:8:4)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:504:3
Now when I run my index.html the JavaScript is writing to the webpage. Here is my index and .js file:
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Learning</title>
</head>
<body>
<h1>JS for Metri</h1>
<p>blah blah blah</p>
<div class="content">
</div>
<!--place scripts at the bottom of the body unless big files. If it's a big files
Metri, make a separate .js file and link it to the HTML page by using src attribute. -->
<script src="test.js" charset="utf-8"></script>
</body>
</html>
test.js
var youLikeMetri = true;
var age= 18;
var enter = 'You are permitted to be legally drunk';
var almost = 'You may legally smoke but not drink'
var exit= 'You are not permitted to be legally drunk';
//if statement if(condition){CODE BLOCK;}
if (youLikeMetri){
document.write("Metri loves you too baby!");
}
//COMPARISON OPERATORS
//==EQUAL TO !=NOT EQUAL TO !==NOT IDENTICAL
//>=GREATER THAN OR EQUAL TO ||OR
//<=LESS THAN OR EQUAL TO &&AND
if (age >= 21){
document.write(enter);
}
//elseif to create multiple conditions after first if.
else if(age <= 20){
document.write(almost);
}
else if (age < 18){
document.write(exit);
}
//while loops are continuous if the condition is met.
// while (condition){CODE BLOCK}
while (age <10) {
console.log('You are less than 10');
age++
//adds one to age when age is > 10 it will continue down.
}
document.write('You are older than 10 now')