This question already has an answer here:
Why doesn't my equality comparison using = (a single equals) work correctly? [duplicate]
(1 answer)
Closed 5 years ago.
I'm trying to run this simple javascript code but doesn't get desired output. This code showing "You are pass" instead of "You are fail". Please tell where im wrong.
<html>
<head>
<title>
If else if and else use
</title>
</head>
<body>
<script>
function ifelseifelse() {
var marks=32;
if (marks>33){
alert("You are Pass");
}
else if(marks=33)
{
alert("You are pass");
}
else{
alert("You are fail");
}
}
</script>
<button type = "button" onclick="ifelseifelse()" >If else-if if</button>
</body>
</html>
You are using
else if(marks=33)
instead of
else if(marks==33)
You are assigning 33 to marks instead of comparing it
Use == in the else-if to compare your marks with 33
it's getting hung up at the second statement.
if(marks=33) should be if(marks === 33)
Related
I have been trying to make a simple trivia game and my console keeps saying "Uncaught SyntaxError: Unexpected identifier
Line: 6" I would greatly apreciate any assistance!
<html>
<head>
<title>Javascript Other Stuff</title>
</head>
<body>
<script>
var question1=prompt("What Does HTML stand for? (lowercase) ")
var question2=prompt("How Many Letters Are In the Alphabet?")
var quesiton3=prompt("How Mant Fingers Do You Have? (number form)")
if question1 = ("hyper-text markup language") {
alert("Correct!")
}
else alert("Incorrect... the correct answer was hyper-text markup language!")
</script>
</body>
</html>
You have mistake in if condition, you must check the variable for the correct string in the condition, it will be correct:
var question1=prompt("What Does HTML stand for? (lowercase) ")
var question2=prompt("How Many Letters Are In the Alphabet?")
var quesiton3=prompt("How Mant Fingers Do You Have? (number form)")
if (question1 === "hyper-text markup language") {
alert("Correct!")
}
else alert("Incorrect... the correct answer was hyper-text markup language!")
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 1 year ago.
Improve this question
I have one webpage ,when loading the page it asks some questions if all the questions are correct only then it displays the body part otherwise the question will not allow next question or it should not displays the body part , please help me to fix this issue...
<!DOCTYPE html>
<html>
<head>
<title>Special-Wishes </title>
<script>
let q1=prompt("what is your name...?"); //if the q1 answer is wrong it should not display the body content
if(q1 == "John" || "JOHN" ){
let q2=prompt("what's your nick name...?");
if(q2=="blabla"){
alert("welcome to the page");
}
}
</script>
</head>
<body>
<h1>My body section</h1>
</body>
The reason why it is still asking the next question is due to your if statement logic
if(q1 == "John" || "JOHN" ) should be if(q1 == "John" || q1 == "JOHN" )
An even easier way of doing this would be if(q1.toUpperCase() == "JOHN")
In order to not display the body, you want to either remove it, or make it hidden. This can be done in a else block after your if statement
Remove: document.body.remove();
Hide: document.body.style.display = "none";
Use document.body.style.display = "none" when condition not matched
let q1 = prompt("what is your name...?"); //if the q1 answer is wrong it should not display the body content
if (q1 == "John") {
let q2 = prompt("what's your nick name...?");
if (q2 == "blabla") {
alert("welcome to the page");
} else {
document.body.style.display = "none"
}
} else {
document.body.style.display = "none"
}
<body>
<h1>My body section</h1>
</body>
This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 2 years ago.
i think that the code 100% normal but why i got that Error????:
Uncaught TypeError: Cannot set property 'innerHTML' of null||
the code is:
<!DoCTYPE html>
<html lang="eng">
<head>
<meta charset ="utf-8">
<meta name="" content="">
<title></title>
<script>
function myAgeInDays() {
"use strict";
var myAge = 15;
return myAge * 365;
}
var daysCalc = myAgeInDays();
document.getElementById("test").innerHTML =
"Your Age In Days = "+ daysCalc+ " Day";
</script>
</head>
<body>
<button onclick="myinfo();">OK</button>
<h1 id="test">Ok</h1>
</body>
</html>
A few things can be done to improve this question:
Give some context, not the error the console prints out
Especially in the case of a very common js error. An example for your case could be "In form submission cannot select html element / selector is null in onclick function".
State some of the things you have tried
This will help you grow as a developer (the old teaching someone to fish story)
Some more tips here: https://stackoverflow.com/help/how-to-ask
In your case, the code you posted does not work (myinfo is not defined) and your code is in the <head> which is defined before the <p> is created. add it at the end of your body to ensure that all the elements are created. If you wanted to make a function handle the button click that contained the logic in your , you would write something like:
<body>
<button id="calcDays">OK</button>
<h1 id="test">Ok</h1>
<script type="text/javascript">
document.getElementById('calcDays')
.addEventListener('click', function () {
function myAgeInDays(days) {
return days * 365;
}
document.getElementById("test").textContent =
`Your Age In Days = ${myAgeInDays(15)} Days`;
});
</script>
</body>
StackOverflow,
I'm a NOOB learning slowly. I got some errors when trying to validate the following code in HTML 5 validator and don't know where the errors are:
<!DoctypeHTML>
<HTML>
<head>
<title> Javascript Programming!</title>
<script type = “text/javascript”>
<function substitute () {
var MyValue = document.getElementID (‘mytextbox’).value;
If (myValue ==0) {
alert(‘please enter a real value in the box’);
Return;
}
Var myTitle = document.getElementbyID (‘title’)
myTitle.innerHTML = myValue;
}
</head>
<body>
</body>
</html>
Errors: Error: Bad value “text/javascript” for attribute type on element script: Expected a token character but saw “ instead.
From line 5, column 2; to line 5, column 34
↩ ↩
Error: End of file seen when expecting text or an end tag.
At line 18, column 7
dy>↩
Error: Unclosed element script.
From line 5, column 2; to line 5, column 34
↩ ↩
Any feedback? Thanks guys and gals.
PreYvin
You are using typographical quotes - change these to regular quotes. (single and double)
Ok, you've got a whole lot of invalid code (HTML and JavaScript) here:
<!DoctypeHTML>
Should be (case doesn't matter):
<!DOCTYPE html>
This:
<script type = “text/javascript”>
contains typographically formatted quotes instead of non-formatted quotes, which is a problem, but you don't even need the type=text/javascript anyway, so you can just write:
<script>
function is not an HTML tag, so this:
<function substitute () {
should be:
function substitute() {
Next, you are using formatted quotes in your JavaScript:
var MyValue = document.getElementID (‘mytextbox’).value;
which should be unformatted, like this:
var MyValue = document.getElementID ('mytextbox').value;
HTML isn't case-sensitive, but JavaScript is, so this:
If (myValue ==0) {
needs to be this:
if (myValue == 0)
More quote problems here:
alert(‘please enter a real value in the box’);
Should be:
alert('please enter a real value in the box');
More case-sensitivity issues here:
Return;
Should be:
return;
More quote and case-sensitivity issues here:
Var myTitle = document.getElementbyID (‘title’)
Should be:
var myTitle = document.getElementbyID ('title');
Lastly, when your script is finished and it's time to return to HTML, you didn't close your script, so this:
}
</head>
Should be:
}
</script>
</head>
You can always validate your HTML at: http://validator.w3.org
And, you can validate your JavaScript at: http://www.jslint.com
You also have invalid JavaScript so this should be valid.
<!doctype html>
<HTML>
<head>
<title> Javascript Programming!</title>
<script>
function substitute () {
var MyValue = document.getElementID (‘mytextbox’).value;
if (myValue ==0) {
alert(‘please enter a real value in the box’);
return;
}
var myTitle = document.getElementbyID (‘title’)
myTitle.innerHTML = myValue;
}
</script>
</head>
<body>
</body>
</html>
you have an extra < in your code. but you need to revisit your javascript as it has many problems the script tag is not closed.
<!DoctypeHTML>
<HTML>
<head>
<title> Javascript Programming!</title>
<script type = “text/javascript”>
function substitute () {
var MyValue = document.getElementID (‘mytextbox’).value;
If (myValue ==0) {
alert(‘please enter a real value in the box’);
Return;
}
Var myTitle = document.getElementbyID (‘title’)
myTitle.innerHTML = myValue;
}
</script>
</head>
<body>
</body>
</html>
Lots of basic syntax errors here.
<!DoctypeHTML> should be <!DOCTYPE html>
the first error you listed, (Bad value “text/javascript” for attribute type on element script: Expected a token character but saw “ instead.) is due to a funky double quote character: “ It should be " This probably originated from your text editor. What are you using? I like Sublime, but there are lots of options. The important thing is that you use a text editor designed for coding.
the next two errors are due to your script tag not being closed. Just add </script> at the end of the script.
Like I said, these are just simple syntax errors though. What you really need to learn here is how to look at those error messages and tell what's going on. Notice how the error messages reference a line number and column number? That's to tell you where the problem is. (Sometimes it can be off depending on the error, but worry about that later). Take a look at the line it's complaining about, read the error message, and you should be able to figure out what's wrong.
Close your <script> tag.
Remove < from <function
Use regular quotes instead of typographical
space between Doctype and html ie. <!doctype html>
Lastly, keywords should be all smallcase ie. if, return, var
Updated
<!doctype html>
<html>
<head>
<title> Javascript Programming!</title>
<script type = 'text/javascript'>
function substitute () {
var MyValue = document.getElementID ('mytextbox').value;
if (myValue == 0) {
alert('please enter a real value in the box');
return;
}
var myTitle = document.getElementbyID ('title')
myTitle.innerHTML = myValue;
}
</script>
</head>
<body></body>
</html>
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 5 years ago.
Improve this question
I'm still new in JavaScript. I am trying to make a program that contains 2 buttons and once a button is clicked, it creates a random number.
The problem is that when I'm trying to compare them, it is not showing which one is bigger. First I thought the problem is that the variables aren't global but it didn't change anything.
Can someone help me find the problem please?
Here is the JavaScript code:
var par1 = document.getElementById("para1");
var par2 = document.getElementById("para2");
var winner = document.getElementById("win");
function button1() {
num1 = Math.floor(Math.random() * 7);
par1.innerHTML = num1;
}
function button2() {
num2 = Math.floor(Math.random() * 7);
par2.innerHTML = num2;
}
if (num1 > num2) {
winner.innerHTML = "the winner is player 1";
} else {
winner.innerHTML = "the winner is player 2";
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>dicestimulator</title>
<link rel="stylesheet" href="dicestimulator.css">
</head>
<body>
<div class="container">
<div>
<h1>Player1</h1>
<button type="button" name="button1" onclick="button1()" id="but1">roll
dice</button>
<p id="para1">Click the button to see what you get</p>
</div>
<div>
<h1>Player2</h1>
<button type="button" name="button2" id="but2" onclick="button2()">roll
dice</button>
<p id="para2">Click the button to see what you get</p>
</div>
<p id="win">let's see who wins!!!</p>
<script src="dicestimulator.js"></script>
</div>
</body>
</html>
if(num1>num2){
winner.innerHTML="the winner is player 1";
}else{
winner.innerHTML="the winner is player 2";
}
You are not calling the above block no where in your HTML code.
My solution for you make a third button that calls the function getwinner
function getWinner() {
if(par1.val>par2.val){
winner.innerHTML="the winner is player 1";
} else{
winner.innerHTML="the winner is player 2";
}
}
Please note that you cannot call local variables created in functions outside those functions. num1 and num2 cease to exist after the scope of the function that created them.
The section of code:
if(num1>num2){
winner.innerHTML="the winner is player 1";
}else{
winner.innerHTML="the winner is player 2";
}
… is not inside a function.
It runs when the script is initially loaded.
Later on, you call the button1 and button2 functions. These change the values of num1.
You never compare the values again.
If you want to compare them when the button1 and button2 functions run, then the code needs to be called when those functions are.