how to use if else in javascript - javascript

In above code i cant display per if i give any condition like if i can i achieve this
and at the end of this code not only if statement bt also any document.write function is not working.
<body>
<script>
var math = Math.floor(window.prompt("Enter Math Marks"));
document.write("Math Marks :"+math);
var eng = Math.floor(window.prompt("Enter English Marks"));
document.write("</br>English Marks : "+eng);
var php = Math.floor(window.prompt("Enter PHP Marks"));
document.write("</br> PHP Marks :"+php);
var java = Math.floor(window.prompt("Enter JAVA Marks"));
document.write("</br> JAVA Marks : "+java);
var csharp = Math.floor(window.prompt("Enter C Sharp Marks"));
document.write("</br> C Sharp Marks : "+csharp);
var total = Math.floor(500);
var obt = Math.floor(math + eng + php + java +csharp);
document.write("<br>"+obt);
var per = Math.floor(document.write("<br>Percentage" (obt * 100)/total));
document.write("nothing is dispaling");
if(per<40){
document.write("Fail");
}
</script>
</body>
</html>

<script type="text/javascript">
<!--
var age = 20;
if( age > 18 ){
document.write("<b>Qualifies for driving</b>");
}
//-->
</script>
More exmples:
<script type="text/javascript">
<!--
var age = 15;
if( age > 18 ){
document.write("<b>Qualifies for driving</b>");
}else{
document.write("<b>Does not qualify for driving</b>");
}
//-->
</script>

Related

function is not invoking in javascript

Can anyone tell what is problem in the following code... when i run the program in browser a blank white screen appears... I don't know why its not working... I am not very sure with the syntax...
I don't want to invoke the function by any events.. i just want to write a function and invoke it by a manual call...
<html>
<head>
<script language="javascript" type="text/javascript">
function salin()
{
var sal = prompt("Enter your current salary - ","");
var in = prompt("Enter the increment % - ","");
sal = parseInt(sal);
in = parseInt(in);
var nsal = sal +( sal*(in /100));
alert("Your new salary is - " + nsal);
}
salin();
</script>
</head>
<body>
</body>
</html>
The issue seems to be in this line in = parseInt(in);
in is a reserved keyword in javascript which is use to return a boolean value. Replace it with a different variable name
I have created this fiddle..
Its working. You were using reserved javascript keyword
function salin()
{
var sal = prompt("Enter your current salary - ","");
var values = prompt("Enter the increment % - ","");
sal = parseInt(sal);
values = parseInt(values);
var nsal = sal +( sal*(values /100));
alert("Your new salary is - " + nsal);
}
salin();
https://jsfiddle.net/abdur_rehman26/7L9uvxon/
Try this instead:
function salin()
{
var sal = prompt("Enter your current salary - ","");
var income = prompt("Enter the increment % - ","");
sal = parseInt(sal);
income = parseInt(income);
var nsal = sal +( sal*(income /100));
return "Your new salary is - " + nsal;
}
alert(salin());
Notice I added return and put the alert() on the function call. I also changed "in" to "income"
'in' is a reserved keyword. change that to some other variable name
<body>
<script language="javascript" type="text/javascript">
function salin()
{
var sal = prompt("Enter your current salary - ","");
var in1 = prompt("Enter the increment % - ","");
sal = parseInt(sal);
in1 = parseInt(in1);
var nsal = sal +( sal*(in1 /100));
alert("Your new salary is - " + nsal);
}
salin();
</script>
</body>

Array sort build in function

I was wondering what's wrong with my program. Am i having a syntax error or am i missing something in my build in array sort?. I'm pretty sure the problem lies within the "for" loop but i cant seem to find it. Some suggestion or help would be great.
<HTML>
<!Foundation Page for building our Javascript programs>
<HEAD>
<TITLE>The Foundation Page </TITLE>
<SCRIPT LANGUAGE = "JavaScript">
function leaderboard()
{
var temp1;
var temp2;
var temp3;
var temp4;
var temp5;
temp1 = 10
temp2 = 20
temp3 = 30
temp4 = 40
temp5 = 50
var leader = new Array(5);
leader[0] = temp1;
leader[1] = temp2;
leader[2] = temp3;
leader[3] = temp4;
leader[4] = temp5;
leader.sort(function(a,b){return b-a});
var myContent = '';
for (var d=0;d<5;d++)
{
myContent += "score: " + leader[d] + "<BR>";
}
document.getElementById("leaderboard").innerHTML = myContent;
}
</SCRIPT>
<HEAD>
<BODY>
<BODY BGCOLOUR = "WHITE">
<H2>The Foundation Page </H2>
<HR>
<SCRIPT LANGUAGE = "Javascript"> leaderboard() </SCRIPT>
</BODY>
</HTML>
You are trying to write the o/p to an element with id leaderboard which is not present in the page. Which is giving an error Uncaught TypeError: Cannot set property 'innerHTML' of null - it is not a syntax error.
So just create an element with the id leaderboard as shown below
<HTML>
<!Foundation Page for building our Javascript programs>
<HEAD>
<TITLE>The Foundation Page </TITLE>
<SCRIPT LANGUAGE = "JavaScript">
function leaderboard()
{
var temp1;
var temp2;
var temp3;
var temp4;
var temp5;
temp1 = 10
temp2 = 20
temp3 = 30
temp4 = 40
temp5 = 50
var leader = new Array(5);
leader[0] = temp1;
leader[1] = temp2;
leader[2] = temp3;
leader[3] = temp4;
leader[4] = temp5;
leader.sort(function(a,b){return b-a});
var myContent = '';
for (var d=0;d<5;d++)
{
myContent += "score: " + leader[d] + "<BR>";
}
document.getElementById("leaderboard").innerHTML = myContent;
}
</SCRIPT>
<HEAD>
<BODY>
<BODY BGCOLOUR = "WHITE">
<H2>The Foundation Page </H2>
<HR>
<div id="leaderboard"></div>
<SCRIPT LANGUAGE = "Javascript"> leaderboard() </SCRIPT>
</BODY>
</HTML>
Demo: Fiddle

Javascript math not working, better way to perform operations?

I included the javascript links above the <head>
I'm wondering about doing this in like one line.
I try this in a basic .html file and nothing shows up, no alert.
I'm probably missing something trivial.
Wait, probably just have to put the dollar signs haha
<!DOCTYPE HTML>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script language="JavaScript" type="text/javascript"></script>
<head>
<style>
</style>
</head>
<body>
<script>
function compute(){
var h = 1595;
var k = 77;
var r = 309;
var rSquared = Math.pow(r,2);
var x = 686;
var group = x - h;
var groupSquared = Math.pow(group,2);
var toBeSquareRoot = rSquared - groupSquared;
var y_neg = 77 + Math.sqrt(toBeSquareRoot);
var y_pos = 77 - Math.sqrt(toBeSquareRoot);
alert(y_pos);
alert(y_neg);
alert(hey);
}
compute();
</script>
</body>
Just a few syntax errors. Ditch the single quotes on the alerts. Also, you were missing the Math. before the pow() method call.
<script>
function compute(){
var h = 1595;
var k = 77;
var r = 309;
var rSquared = Math.pow(r,2);
var x = 686;
var group = x - h;
var groupSquared = Math.pow(group,2);
var toBeSquareRoot = rSquared - groupSquared;
var y_neg = 77 + Math.sqrt(toBeSquareRoot);
var y_pos = 77 - Math.sqrt(toBeSquareRoot);
alert(y_pos);
alert(y_neg);
alert('hey');
}
compute();
</script>
Your code is returning NaN. This is probably due to a negative number being passed into the .sqrt() method. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sqrt

Compare and Output 2 strings in JavaScript

I am new to JavaScript, and I'm trying to make a web page where if I have the same open and closing hours for 5 days a week, it will output them on the same line. For example, the hours for Monday - Friday are the same in my example so it would print out:
Monday - Friday: 6:00AM - 2:00PM
Saturday 8:00AM - 1:00PM
Sunday Closed (Without the hyphen)
When I run this in my browser, I get a blank page. Here is my code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<script>
var MondayOpen = "6:00AM - ";
var MondayClose = "2:00PM";
var TuesdayOpen = "6:00AM - ";
var TuesdayClose = "2:00PM";
var WednesdayOpen = "6:00AM - ";
var WednesdayClose = "2:00PM";
var ThursdayOpen = "6:00AM - ";
var ThursdayClose = "2:00PM";
var FridayOpen = "6:00AM - ";
var FridayClose = "2:00PM";
var SaturdayOpen = "8:00AM - ";
var SaturdayClose = "1:00PM";
var SundayOpen = "Closed";
var SundayClose = "";
var dateArray = new Array(MondayOpen, MondayClose, TuesdayOpen, TuesdayClose, WednesdayOpen, WednesdayClose, ThursdayOpen, ThursdayClose,FridayOpen, FridayClose, SaturdayOpen, SaturdayClose, SundayOpen, SundayClose);
function outputDate(){
for(int i = 0; i<dateArray.length; i++){
var current;
//Puts Open and Close time into 1 string
dateArray[i] + dateArray[i+1] = current;
//Compare the two strings
if(current.substring(0,15) == current.substring(0,15) +2)
}
$("#hours").html(<b> current <b/>);
}
var getHours = document.getElementById('hours').innterHTML = current;
}
</script>
<div id= "hours" style="font-size:10px; color:#fff;">
</div>
What am I missing here? Any help would be appreciated.
There are several mistakes here. Just to name a few:
You put int instead of var here:
for(var i = 0; i<dateArray.length; i++){
You put String instead of var here:
var curr;
You're assigning a variable to an expression? Did you mean to do it the other way around?
//Puts Open and Close time into 1 string
curr = dateArray[i] + dateArray[i+1];
You have a closing brace after your if statement...
//Compare the two strings
if(current.substring(0,15) == current.substring(0,15) +2)
{
You type innterHTML instead of innerHTML and try to do multiple assignments:
var getHours = current;
document.getElementById('hours').innerHTML = current;
You forget to enclose this in quotes:
$("#hours").html("<b> current <b/>");
EVEN after fixing all these syntax errors I get a blank page. Consider rewriting it from scratch.
In Javascript you don't need to declare String theString = "This is a string".
Just write var stringName = "This is my string."
You might want to take a step back and master JavaScript and HTML. There are many free resources available. Just to be brief, why not try Code Academy or W3 schools. That being said, here's enough of a fix to display your variables.
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
</head>
<body>
<div id="hours"></div>
</body>
<script type="text/javascript">
var MondayOpen = "6:00AM - ";
var MondayClose = "2:00PM";
var TuesdayOpen = "6:00AM - ";
var TuesdayClose = "2:00PM";
var WednesdayOpen = "6:00AM - ";
var WednesdayClose = "2:00PM";
var ThursdayOpen = "6:00AM - ";
var ThursdayClose = "2:00PM";
var FridayOpen = "6:00AM - ";
var FridayClose = "2:00PM";
var SaturdayOpen = "8:00AM - ";
var SaturdayClose = "1:00PM";
var SundayOpen = "Closed";
var SundayClose = "";
var dateArray = new Array(MondayOpen, MondayClose, TuesdayOpen, TuesdayClose, WednesdayOpen, WednesdayClose, ThursdayOpen, ThursdayClose,FridayOpen, FridayClose, SaturdayOpen, SaturdayClose, SundayOpen, SundayClose);
function outputDate(){
var printOutResults;
for(i = 0; i<dateArray.length; i++) {
var current;
//Puts Open and Close time into 1 string
current = dateArray[i] + dateArray[i+1];
printOutResults += current;
//Compare the two
if(current.substring(0,15) == current.substring(0,15) +2){
$("#hours").html(current);
}
}
$("#hours").html(printOutResults);
}
outputDate();
</script>

multiple matches in regex

Hi I am trying to get the following javascript code to work. When I am typing a line containing all three names
fever, nightsweats and nocturia - I want an alert window to come on reminding me that those three in combination represent a dangerous sign. what am I doing wrong? thanks so much in advance!!!
<html>
<script type="text/javascript">
function show_prompt() {
var q = prompt("Please enter your name", "");
var myRegExp1 = ^ ( ? = . * fever)( ? = . * night sweats)( ? = . * nocturia);
var myRegExp2 = /fever,weight-loss,nocturia|weight-loss,fever,nocturia|nocturia,weight-loss,fever/
var string1 = q;
var string2 = q;
var matchPos1 = string1.search(myRegExp1);
var matchPos2 = string2.search(myRegExp2);
if (matchPos1 != -1) {
alert("bingo" + matchPos1);
}
if (matchPos2 != -1) {
alert("symptoms" + matchPos2);
}
}
</script>
<body>
<input type="button" onclick="show_prompt()" value="Show prompt box" />
</body>
</html>
For one, you have a syntax error :
var myRegExp1 = ^ ( ? = . * fever)( ? = . * night sweats)( ? = . * nocturia);
Needs to be
var myRegExp1 = /^ ( ? = . * fever)( ? = . * night sweats)( ? = . * nocturia)/;
If I understand correctly, I think this is what you want. Please test it here.
function show_prompt() {
var q = prompt("Please enter your name", "");
var myRegExp = /(fever|weight-loss|nocturia),(fever|weight-loss|nocturia),(fever|weight-loss|nocturia)/;
var string = q;
var matchPos = string.search(myRegExp);
if (matchPos != -1) {
alert("symptoms" + matchPos);
}
}

Categories