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!")
Related
I wanting to make an HTML app that sends you to a certain website depending on the number of spaces in a text box. Here is what I am thinking:
If there are no spaces, go to google.com.
Else, go to bing.com.
I found this for getting the word count:
Count = myText.trim().replace(/\s+/g, ' ').split(' ').length;
What should I do from here? I tried making an if statement but got a plethora of errors. All help is appreciated as I am new to HTML. Thank you in advance!
I see that im posting late...
Try the following, I hope this helps you out :)
function submit(){
// Get input contents
var content=document.getElementById("wierd").value;
// Get space count
var count=(content.match(/ /g) || []).length;
// Redirect the user
// In this example, we dont actually redirect the user since this is quite annoying.
if(count===0){// If theres no spaces
console.log("duckduckgo.com");
}else{
if(count===1){// If theres 1 space
console.log("yandex.com");
}else{
if(count===2){// If theres 2 spaces
console.log("yahoo.com");
}else{//If not a choice
console.log("unknown.com");
}
}
}
}
/*ALL SO related*/
body{
background:#121212;
font-family:Helvetica, sans-serif;
}
input,button{
padding:4px;
margin:5px;
background:#333333;
border:1px solid grey;
color:white;
outline:none;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<input type="text" id="wierd">
<button onclick="submit();">Fire up useless function</button>
</body>
<script src="script.js"></script>
<html>
here is my solution and I didn't use your way of finding word count :
// select your text box
let text = document.getElementById('someId').value;
// if there is only 1 element in the array then there is no space and redirect to google if not go to bing
if( text.split(' ').length > 1 ){
window.location = 'https://www.google.com';
} else {
window.location = 'https://www.bing.com';
}
I am making an array from the text in the input by splitting it using the space
example :
text = 'aaaa aaaa aa' ====> going to be ['aaaa', 'aaaa', 'aa']
if there is more than 1 element then there is 1 space at least and we proceed to the if statement
A way to do it:
function redirect(str) {
var count = str.trim().replace(/\s+/g, ' ').split(' ').length - 1;
count == 0 ?
window.open('https://www.google.com/') :
window.open('https://www.bing.com/') // :
// you can add more conditions like count == 1 ? {direction} : {else direction}
}
Even though this is completely weird, and probably useless:
function getSite() {
var lookup = {"":"www.google.com", " ": "www.bing.com"};
var result = document.getElementById("weirdo").value;
// window.open(lookup[result]);
// For demo, commented out and logging instead
alert(lookup[result]);
}
<input id="weirdo" type="text"/>
<button type="button" click="getSite()">
Enter text and click me!
</button>
Okay, I'm trying to make a cheesy accent generator to practice with RegEx. But I have a strange problem that seems unrelated to RegEx. The submit button doesn't do anything. At first the function "maccent" was just called "accent" and at that time the console said "accent" was not a function. With nothing better to go on, I assumed it was because the word "accent" was used so many other times, so I changed the function name to "maccent". Now, however, nothing happens. What's the deal? Here is my code:
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>Accent Generator</title>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
</head>
<body>
<p>Choose an accent</p>
<input type = "text">
<form action="">
<input type="radio" name="accent" value="German"> German<br>
<input type="radio" name="accent" value="English"> English<br>
<input type="radio" name="accent" value="Indian"> Indian
</form>
<button type="submit" onclick = "maccent()">Submit</button>
<div id = "accented"></div>
<script>
var accent = $('input[name="accent"]:checked').val();
function maccent()
{
if (accent == "German")
{
germAcc();
}
}
function germAcc()
{
var sample = $("input").val()
var desire = sample.replace(/[w]/gi, "v")
//not if it's at the end of a word
var desire2 = desire.replace(/th/, "z")
//replace h too, but not with another z.
//wait, what? It replaces t even if its not followed by h
var desire3 = desire2.replace(/er/, "a")
//this is going to be a hard one
//er is replaced with a but only if its followed by a space or a punctuation
mark.
console.log(desire3);
}
function indAcc()
{
var sample = $("input").val()
var desire = sample.replace(/[r]/gi, "d")
//not if it's at the end of a word
//this words, but not on each indivual word
console.log(desire);
}
function itAcc()
{
}
function britAcc()
{
var sample = $("input").val();
var desire = sample.replace(/[a]/gi, "au")
var desire2 = desire.replace(/er/, "a")
//this is going to be a hard one
console.log(desire2);
//not if it's at the end of a word
}
</script>
</body>
</html>
The problem is the assignment of the "variable" accent. You are doing it at global scope (the top level), so it gets assigned when the page is first loaded.
If you move that assignment into the function maccent() (and move the work "mark" back into the comment it belongs to), your page will work.
Incidentally, the old problem was that you had a function and a variable trying to share the name accent. The variable was "winning".
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>
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)
I'm learning a bit HMTL5 to prepare to the 70-480 exam. I'm trying to do some javascript code. It looks something like this:
function inchestometers(inches) {
if (inches < 0)
return -1;
else {
var meters = inches / 39.37;
return meters;
}
}
var inches = 12;
var meters = inchestometers(inches);
document.write("the value in meters is " + meters);
var hello = document.getElementById("hello");
hello.firstChild.nodeValue = "Hello World";
and I have such html code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Htnl 5 test</title>
<script src="script/test.js"></script>
</head>
<body>
<p id="hello">Hello</p>
</body>
</html>
In my VS 2012 i have used the Asp.net Empty Web application project and added the Js file and also the html file. The problem is that The function runs properly without any exeptions. This function is taken from here: http://msdn.microsoft.com/en-us/library/cte3c772(v=vs.94).aspx
But whem I'm trying to run the code where I'm getting the document element it' crashint with the error like in the subject. What I've investigated is that the hello gets the null value. I've also tried the code thaken from here:
http://msdn.microsoft.com/en-us/library/yfc4b32c(v=vs.94).aspx - the example with the div. I have the same effect.
What is wrong? I know that there were simmilar subjects but I can't seem to find one matching to mine. Thank you kindly for your help.
Regards
Rafal
you are getting a problem because your javascript code is running before the element
<p id="hello">
is defined.
the simplest solution is to include your script at the end of the body section instead of in the head section but this would cause the document.write call to occur after the rest of the content.
another solution would be to place the code inside two functions like this
function do_conversion() {
var inches = 12;
var meters = inchestometers(inches);
document.write("the value in meters is " + meters);
}
function say_hello() {
var hello = document.getElementById("hello");
hello.firstChild.nodeValue = "Hello World";
}
then change the body section like this
<body onload='say_hello()'>
<script>
do_conversion();
</script>
<p id="hello">Hello</p>
</body>