so I have lab that I am trying to do and my professor wants it to be done in two separate files. Unfortunately she is one of the worst CS teachers I've ever had so I pretty much have to teach myself everything. I was able to get my code to work in one html file by putting all of my js code in script tags. I am pretty sure I am linking the files correctly but then again I am not sure. My html and js file are located in the same directory as well. Here is my code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="author" content="David Smith">
<meta name="discription" content="lab7">
<meta name="keywords" content="lab7">
<title>Lab7</title>
<link type="text/css" rel="stylesheet" href="lab7.css" />
<script type="text/javascript" src="lab7.js"></script> <!-- I tried linking here -->
</head>
<body>
<script type="text/javascript" src="lab7.js"></script> <!-- and tried linking here -->
<div>
<span id="firstNum"></span>
<span id="op"></span>
<span id="secondNum"></span>
<span>=</span>
<span><input class="textbox" id="textbox" type="text"></span>
<span><input id="button" type="submit" name="button" value="enter" onclick="button();" /></span>
<span id="timer"></span>
</div>
<div>
<span>Number correct: </span>
<span id="numCorrect">0</span>
</div>
<div>
<span><input id="button2" type="submit" name="button" value="try again!" onclick="button2();" /></span>
</div>
</body>
</html>
js file:
var ops = ['+', '-', '*'];
var timeLeft = 30;
var timer = document.getElementById('timer');
var firstNum = document.getElementById('firstNum');
var secondNum = document.getElementById('secondNum');
var op = document.getElementById('op');
var randomOp = ops[Math.floor(Math.random() * 3)];
var randomFirstNum = Math.floor(Math.random() * 10 + 1);
var randomSecondNum = Math.floor(Math.random() * 10 + 1);
firstNum.innerHTML = randomFirstNum;
op.innerHTML = randomOp;
secondNum.innerHTML = randomSecondNum;
var timerId = setInterval(countdown, 1000);
var currentCorrectAnswers = 0;
document.getElementById('button').addEventListener("click",
function () {
var answer = document.getElementById("textbox").value;
var numCorrect = document.getElementById("numCorrect");
if (randomOp == '+') {
if (randomFirstNum + randomSecondNum == answer) {
document.getElementById("textbox").style.backgroundColor = "white";
currentCorrectAnswers += 1;
randomOp = ops[Math.floor(Math.random() * 3)];
randomFirstNum = Math.floor(Math.random() * 10 + 1);
randomSecondNum = Math.floor(Math.random() * 10 + 1);
firstNum.innerHTML = randomFirstNum;
op.innerHTML = randomOp;
secondNum.innerHTML = randomSecondNum;
} else {
document.getElementById("textbox").style.backgroundColor = "red";
}
} else if (randomOp == '-') {
if (randomFirstNum - randomSecondNum == answer) {
document.getElementById("textbox").style.backgroundColor = "white";
currentCorrectAnswers += 1;
randomOp = ops[Math.floor(Math.random() * 3)];
randomFirstNum = Math.floor(Math.random() * 10 + 1);
randomSecondNum = Math.floor(Math.random() * 10 + 1);
firstNum.innerHTML = randomFirstNum;
op.innerHTML = randomOp;
secondNum.innerHTML = randomSecondNum;
} else {
document.getElementById("textbox").style.backgroundColor = "red";
}
} else {
if (randomFirstNum * randomSecondNum == answer) {
document.getElementById("textbox").style.backgroundColor = "white";
currentCorrectAnswers += 1;
randomOp = ops[Math.floor(Math.random() * 3)];
randomFirstNum = Math.floor(Math.random() * 10 + 1);
randomSecondNum = Math.floor(Math.random() * 10 + 1);
firstNum.innerHTML = randomFirstNum;
op.innerHTML = randomOp;
secondNum.innerHTML = randomSecondNum;
} else {
document.getElementById("textbox").style.backgroundColor = "red";
}
}
numCorrect.innerHTML = currentCorrectAnswers;
});
document.getElementById('button2').addEventListener("click",
function () {
window.location.reload(true);
});
function countdown() {
if (timeLeft == -1) {
//timeLeft = 30;
clearTimeout(timerId);
alert("you answered " + currentCorrectAnswers + " questions correctly!");
} else {
timer.innerHTML = 'time left: ' + timeLeft + ' seconds';
timeLeft--;
}
}
Can someone please let me know what I am doing wrong? Thanks!