<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Exchange rate</title>
<link rel="stylesheet"type="text/css"href="exstyle.css"/>
</head>
<body>
<script type="text/javascript">
d=35;
e=40;
x=prompt("Insert Value");
var z=x/d,
g=x/e;
var currency;
currency=prompt("Insert Country");
if(currency="dollar"){
document.write(+z);
}
else
if(currency="euro"){
document.write(+g);
}
</script>
</body>
</html>
When I try to run this code, the result always the same.
Example I input 4000 as a value of x and when I insert currency as dollar I got 114.28 which is correct but when I insert currency as euro the result still the same as dollar. did I do something wrong?
You need to do the comparison operator (==)
if(currency=="dollar"){
document.write(+z);
}
else
if(currency=="euro"){
document.write(+g);
}
Related
I am a noob for learning javascripts
Here I have a javascripts code
I made a button named "click"
when you click the button
it will send a value xx=1 to x
if the value is match with the condition
then it will print "Y", otherwise it will be "N"
but it always cannot shows the messagebox.
<html>
<head>
<title>test</title>
<script language="javascript">
function jclass(xx)
{
var x=xx;
if x==1
alert("Y");
else
alert("N");
}
</script>
</head>
<head>
<h1><a href="javascript:jclass(1);">click</h1>
</head>
</html>
I have also tried other ways like this.
but it still cannot work.
<html>
<head>
<title>test</title>
<script language="javascript">
function jclass()
{
var x=1;
if x==1
alert("Y");
else
alert("N");
}
</script>
</head>
<head>
<h1><a href="javascript:jclass();">click</h1>
</head>
</html>
You have syntax error in if condition. The basic if...else syntax requires condition inside parenthesis after if.
function jclass(xx){
var x=xx;
if (x==1)
alert("Y");
else
alert("N");
}
<h1><a href="javascript:jclass(1);">click</h1>
You must use the brackets
if(x == 1)
You are missing the brackets for comparison,its not python:)
You can check these type of errors in console and act upon exact statement
You should also put curly braces even for a single line in a block or condition to avoid any legacy or formatting issues when running code
Happy Programming
<html>
<head><title>test</title>
<script language="javascript">
function jclass(xx)
{
var x=xx;
if (x==1) {
alert("Y");
}
else {
alert("N");
}
}
</script>
</head>
<head>
<h1><a href="javascript:jclass(1);">click</h1>
</head>
</html>
I have a function that returns some HTML fragment that I store in a variable called data, with its whole structure. What I want is to extract from it some of those parts.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script id="hello">
</script>
</head>
<body>
</body>
</html>
For example, I want to get the body and save it in a new variable:
var body = data.split("<body")[1].split(">").slice(1).join(">").split("</body>")[0];
Where data is the HTML text as a string that the original function is returning.
Is there any way I could save an specific script, from its ID (in this case with id = hello), and save it in another variable??
Thank you very much
var newVar = $("#hello").html();
Let's suppose you have an HTML string in a variable, for example
var foo = '<body><span>bar</span></body>';
Now, let's initialize a parser, to convert this into HTML:
var parser = new DOMParser();
var doc = parser.parseFromString(foo, "text/html");
Now, you can read anything from foo, as it is converted into HTML:
document.getElementsByTagName("body")[0].innerHTML = doc.querySelectorAll("body")[0].innerHTML;
$html = document.querySelector("body").innerHTML;
$hello = document.getElementById("hello").innerHTML;
console.log($html);
console.log($hello);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script id="hello">
// script data
</script>
</head>
<body>
</body>
</html>
I am working on the base for a simple Russian roulette simulator. Here's the code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>RR</title>
</head>
<body>
<button onclick=load()>Load</button>
<button onclick=fire()>trigger</button>
<script>
var slot=0;
var load=function(){
slot=Math.Floor(Math.random()*6+1);
};
var fire=function(){
slot=slot-1;
if(slot===0){
confirm("BANG!!");
}else{
if(slot<0){
confirm("There is no bullet in this gun.");
}
}
};
</script>
</body>
</html>
When I click load I get the error in the title. I know Math.Floor is a function because I've used it before and I looked it up. Did I mess something up in the code? Thanks.
Change...
slot=Math.Floor(Math.random()*6+1);
to...
slot=Math.floor(Math.random()*6+1);
As Roland and Sterling pointed out, the floor function starts with a lower case f letter.
I'm new to JavaScript and already encountered a problem. When I run the code and the browser pops up, it[browser] does not show anything. What I have is the testMethod.js file with one method:
function testMethod(num1, num2){
var value = num1 + num2;
return value;
}
and an HTML file from where I'm trying to run:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title> My JavaScript</title>
<script language = "javascript" src = testMethod.js"></script>
</head>
<body>
<script language = "javascript" type = "text/javascript">
// var getValue = testMethod(2,3);
document.write("The result is " + testMethod(5,3));
</script>
<noscript>
<h3> This site requires JavaScript</h3>
</noscript>
</body>
</html>
The code is not implementing the result at all. It shows only a blank page browser.
It seems you have a quote missing in the html, it should say src="testMethod.js" where you are including the script in the first place.
<html>
<head></head>
<body>
<script>
var colors = new Array();
var count = colors.push(“red”, “green”);
alert(count);
</script>
</body>
</html>
I tried this on firefox and IE, do you think that my version of JavaScript needs to be updated?
You need to use real quotes, either " or ', for example:
var count = colors.push('red', 'green');
The quote character you have used is illegal and is showing the JavaScript error SyntaxError: Unexpected token ILLEGAL.
Demo:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>demo</title>
<script type='text/javascript'>
var colors = new Array();
var count = colors.push('red', 'green');
// alert(count);
alert(colors[0]);
</script>
</head>
<body>
</body>
</html>
I just copy pasted your code in jsFiddle and it looks like your quotes are illegal characters (copy pasted from word / web?). Try typing them out again and it should work.