Uncaught ReferenceError: document is not defined [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
So I have a simple app to help a diabetic calculate some different things they would need to help regulate their blood sugar. At the bottom of the page I want to state a few different things as a quick overview. So when I try and pull Carbs down to my list, it doesnt work. What do i do? Code is shown below
<!DOCTYPE HTML>
<html lang="en-us">
<head>
<meta charset="utf-8" name="Austin" content="HTML Code and Java Script">
<title>Title</title>
<script type="text/javascript">
function doseToEat() {
var carbs = parseFloat(document.getElementById("carbs").value);
var insulin = parseFloat(document.getElementById("insulinUnitPer10Carbs").value);
var dose = carbs / (insulin * 10)
document.getElementById("dose").innerHTML = dose;
documnet.getElementById("carbsOutput").innerHTML = carbs;
}
function correction() {
var currentBS = parseFloat(document.getElementById("currentBS").value);
var correction = (currentBS - 100) / 30
document.getElementById("correction").innerHTML = correction;
document.getElementById("currentBSOutput").innerHTML = currentBS;
}
</script>
</head>
<body>
<h3>This Is to help calculate and track your blood sugar.</h3>
<input type="number" id="currentBS">Please Enter your current Blood Sugar.
<br>
<button type="button" onclick="correction()">Calculate Correction</button>
<br>
<label>This is your correction.</label>
<div id="correction"></div>
<br>
<input type="number" id="carbs">How many Carbs will you be eating?
<br>
<input type="number" id="insulinUnitPer10Carbs">How many Units of Insulin do you need per 10 carbs?
<br>
<button type="button" onclick="doseToEat()">Calculate</button>
<br>
<label>This Is How much Insulin you should dose</label>
<div id="dose"></div>
<output type="number" id="time">Time:</output>
<br>
<output type="number" id="Date">Date:</output>
<br>Current Blood Sugar:
<output type="number" id="currentBSOutput"></output>
<br>Carbs:
<output type="number" id="carbsOutput"></output>
<br>
</body>
</html>

You have miss spelled document, need to change this
documnet.getElementById("carbsOutput").innerHTML = carbs;
^------^
to
document.getElementById("carbsOutput").innerHTML = carbs;

Related

Why i'm getting NaN in alert? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm adding two numbers, but I am getting NaN in the alert. For example, doing 10+10 returns NaN in the alert. What am doing wrong in this code?
function calcy(){
let a = document.getElementById('in1').Value;
let b = document.getElementById('in2').Value;
let total = parseInt(a) + parseInt(b);
alert(total);
}
<div class="inputs">
<div class="input1">
<input type="text" id="in1">
</div>
<div class="input2">
<input type="text" id="in2">
</div>
</div>
You need to change the .Value to .value;
Try below code :
<script>
function calcy(){
let a = document.getElementById('in1').value;
let b = document.getElementById('in2').value;
console.log("a ="+a);
console.log("b ="+b);
let total = parseInt(a) + parseInt(b);
console.log("total ="+total);
alert(total);
}
</script>
<div class="inputs">
<div class="input1">
<input type="text" id="in1">
</div>
<div class="input2">
<input type="text" id="in2">
</div>
<button onclick="calcy()" >Click</button>
</div>

Displaying Complex Numbers as an Answer

I appreciate any help. I am writing this as a project I have in Pre-Cal. I am tasked with writing a webpage, based in notepad that will run the quadratic equation with the user inputting the variables. I have never written code before so while this is probably simplistic, it has taken me 3 weeks. The issue is correctly displaying complex number answers. I tried linking to "math.js" at the top but it had no effect. Here is what I am working with (I took off the explanations and bells and whistles) :
<html>
<head>
<script src="https://unpkg.com/mathjs#6.6.4/dist/math.js type="text/javascript"></script>
</head>
<body>
<label> Enter Variable "a"</label> <br>
<input type="text" id="a"> <br>
<label> Enter Variable "b"</label> <br>
<input type="text" id="b"> <br>
<label> Enter Variable "c"</label> <br>
<input type="text" id="c"> <br>
<input type="button" value="Solve" onclick="quad()"><br>
The Discriminant is:
<div id="dis"></div>
The 1st Root (also called zero) is:
<div id="res"></div>
The 2nd Root (also called zero) is:
<div id="res2"></div>
*Note that if the answers are not real numbers (a negative discriminant) the answer will read "NaN"*
<script>
function quad() {
var a = document.getElementById("a").value;
var b = document.getElementById("b").value;
var c = document.getElementById("c").value;
var Discriminant = (b*b-4*a*c);
var sroot = Math.sqrt(Discriminant);
var denominator =2*a;
var solution1 =(-b-sroot)/denominator;
var solution2 =(-b+sroot)/denominator;
document.getElementById("dis").innerHTML = Discriminant;
document.getElementById("res").innerHTML = solution1;
document.getElementById("res2").innerHTML = solution2;
}
</script>
</body>
</html>

how work activate function event onclick(or other function) when it is activated from browser? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Some day ago i'd write for asking what was the problem about onclick event doesn't activate when i clicked in a specific area of the browser. After check a guide i find it the problem end also the rappresentation of my simply test is trivial, i've known the mechanism for activating a particular function in browser text area.
Now that work it... i'd want add more detail of my question...what is the mechanism that activate my function from template html to tag <script></script> ?
<h3> Check is palindrome</h3>
<input type="text" id="input1" >
<input type="text" id="input2">
<br><br>
<input type="button" id="reload" onclick="myFunction()" value="ricarica">
<body >
<p onclick="check()" id="output" style="line-height: 1000px"> TEST</p>
</body>
//WHAT IS THE MECHANISM IN THIS POINT FOR ACTIVATE check()?
<script>
function check() {
var x = document.getElementById("input1").value;
var y = document.getElementById("input2").value;
if(x===y){
document.getElementById("output").innerHTML=" E' PALINDROMA "
}else document.getElementById("output").innerHTML=" NON E' PALINDROMA "";
}
function myFunction() {
location.reload(true);
}
</script>
</html>
Everything about your code is wrong. Like... literally everything. Here's a suggested alternative:
var out = document.getElementById('output');
document.getElementById('input').oninput = function() {
if( this.value.split('').reverse().join('') === this.value) {
out.innerHTML = "The input is a palindrome!";
}
else {
out.innerHTML = "The input is not a palindrome...";
}
};
<input type="text" id="input" />
<p id="output">Type something...</p>
From what I understand, move your check() function to the input field
<input type="text" id="text1" onclick="check()">
and remove from body
<body>

My script returns wrong value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
When I enter "100" for "weight" and "2" for "height", I receive "0.0002" as the result and this is obviously wrong:
$(document).on('ready page:load', function() {
$('#butt').click(function(){
var hei = $("#hei").val();
var wei = $("#wei").val();
bmi = hei/(wei*wei);
var result = $("#result");
result.html("Your BMI is " + bmi);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="bmi">
<h3>Step 1: Calculating Your Body Mass Index (BMI)</h3>
<form>
<label>Enter Your weight (Kg)</label>
<input type="text" name="weight" id="wei" value="">
<br>
<label>Enter Your height (M):</label>
<input type="text" name="height" id="hei" value="">
<br>
<button type="button" id="butt">Calculate my BMI</button>
<p id="result">Your BMI is </p>
</form>
</div>
The problem isn't the script, but that you swapped your variables;
bmi = hei/(wei*wei);
should be
bmi = wei/(hei*hei);
Your program is correct. Your formula is wrong.
BMI = kg/M^2
So, your formula should be:
bmi = wei/(hei*hei);

Button doesn't trigger an event [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Please help me with the following code because I don't know what I am missing. A click on button Compute should trigger function computeAll(). Instead the script doesn't react to my actions:
the html code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<script src="complex.js"></script>
</head>
<body>
<form name="calcul" method ="post">
<table>
<tr><th></th><th>Real part</th><th>Imaginary part(i)</th></tr>
<tr><td>Enter the first complex number</td><td><input type="text" name="real1"/></td>
<td><input type="text" name="imaginary1"/></td></tr>
<tr><td>Enter the secound complex number</td><td><input type="text" name="real2"/></td>
<td><input type="text" name="imaginary2"/></td></tr>
<tr><td>Enter your choice</td><td colspan="2"><div><p>Addition<input type="radio" value="add" checked name="option">
</p><p>Substraction<input type="radio" value="subs" name="option"></p></div>
</td></tr>
<tr><td >The answer is:</td><td><input type="text" name="displayReal"/></td><td><input type="text" name="displayImaginary"/></td></tr>
<tr><td colspan="3"></td></tr>
<tr>
<td><input type="button" value="Compute" onclick="computeAll()"></td>
<td><input type="button" value="Clear" onclick="clearAll()"></td>
</tr>
</table>
</form>
</body>
</html>
and java script code
<script type="text/javascript">
function computeAll() {
var val1, val2, img1, img2, resultR, resultI
val1 = parseInt(document.calcul.real1.value);
val2 = parseInt(document.calcul.real2.value);
img1 = parseInt(document.calcul.imaginary1.value);
img2 = parseInt(document.calcul.imaginary2.value);
if (calcul.option[0].checked){
resultR = val1 + val2;
resultI = img1 + img2;
document.calcul.displayReal.value = resultR;
document.calcul.displayImaginary.value = resultI;
}
if (calcul.option[1].checked){
resultR = val1 - val2;
resultI = img1 - img2;
document.calcul.displayReal.value = resultR;
document.calcul.displayImaginary.value = resultI;
}
}
</script>
Where is that <script> tag located? In the complex.js file referenced in <head>? You shouldn't need a <script> tag in a JavaScript file, which might be causing the issue. Check your browser's developer tools for any JS errors.
You don't need to put the javascript code inside <script type="text/javascript"> and </script> when you referenced as external file (as in <script src="complex.js"></script>). Delete that, and try again.

Categories

Resources