function is not invoking in javascript - 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>

Related

how can i make my calculation using math.sqrt right?

I just want to get the square root of total2 .. but it won't appear in the selected box ..
here is the javascript codes.
i'll comment the html codes.
function myFunction() {
var q1 = document.getElementById("qinput1").value;
var q2 = document.getElementById("qinput2").value;
var q3 = document.getElementById("qinput3").value;
var total = parseInt(q1) + parseInt(q2) + parseInt(q3);
document.getElementById("ainput3").value=total;
var a1 = document.getElementById("ainput1").value;
var a2 = document.getElementById("ainput2").value;
//from the total we got, lets assign it a variable for further calculation
var a3 = document.getElementById("ainput3").value=total;
var total2 = parseInt(a1)*parseInt(a2)/ parseInt(a3);
document.getElementById("ansA").value = total2;
var total3 = math.sqrt(parseInt(total2));
document.getElementById("sqaureD").value = total3;
}
function myShapes() {
document.getElementById('squareA').style.display =
document.getElementById('shapes').value == 'Square' ? 'block' : 'none'
}
<form action="" id="fcalculation">
<fieldset>
<legend>Calculation of qu</legend>
<label><i>Ultimate bearing capacity</i> <b>(qu) = </b></label>
<input id="qinput1" type="text" placeholder="c'NcFcsFcdFci"/> +
<input id="qinput2" type="text" placeholder="qNqFqsFqdFqi"/> +
<input id="qinput3" type="text" placeholder="½βγNFγsFγdFγi"/>
</fieldset>
</form>
it seems that the calculation part at the very end is not working. sorry its my first time to code.
Classname is Math not math
Try replacing
var total3 = math.sqrt(parseInt(total2,10));
with
var total3 = Math.sqrt(parseInt(total2,10));
Also, looking at your markup, there are no fields with id ainput1, ainput2 and ainput3.

Javascript blunder

I'm attempting to create a webpage that will allow my employees to enter a number and get a number that has run through an equation. I want it to do two simple math problems as follows and output the number that's larger.
Equations
x+150=y
x*1.5+89=z
Then display the larger variable.
I can't get it to work.
I'm pretty sure it's a major noob mistake.
<script type="text/javascript">
function updateOutput() {
//get form
var form = document.getElementById("calc");
//get output
var out = form.elements["z"];
//get two numbers
var num1 = parseInt(form.elements["x"].value);
//add 150
var num2 = 150;
//multiply 1.5;
var num3 = 1.5;
//add 89
var num4 = 89;
//amount1
var amount1;
//amount2
var amount2;
//set output depending on amount
//add
amount1.value = num1+num2;
//multiple
amount2.value = num1*num3+num4;
If amount1 > amount2 Then
out.value = amount1.value
Else
out.value = amount2.value
}
</script>
Some errors:
amount1.value only works if you define amount as Object, and it doesn't need to be object here. Same for amount2.
if else notation error
Better go to some tutorial sites like w3school or codecademy or buy some books.
Changes of your code, with added form, input to demonstrate.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="calc">
<input type="number" name="x"/>
<input type="number" name="z"/>
</form>
<button onclick="updateOutput();">click</button>
<script>
function updateOutput() {
//get form
var form = document.getElementById("calc");
//get output
var out = form.elements["z"];
//get two numbers
var num1 = parseInt(form.elements["x"].value);
//add 150
var num2 = 150;
//multiply 1.5;
var num3 = 1.5;
//add 89
var num4 = 89;
//amount1
var amount1;
//amount2
var amount2;
//set output depending on amount
//add
// It's ok to just assign value to them.
amount1 = num1+num2;
//multiple
amount2 = num1*num3+num4;
// Also here, don't use amountX.value.
if (amount1 > amount2) {
out.value = amount1
} else {
out.value = amount2
}
}
</script>

How can i get a variable to change number depending on what the user has eneterd

So i am creating a simple dice game which you enter the guess of the dice roll and amount you wish to bet. I cant seem to figure out how i can get my balance to change when a user enters a number.
My code:
//declaring global variables
var diceroll = 0;
var balance = 1000;
var stake = 0;
var guess = 0;
while (balance <= 0) {
var guess = document.getElementById("guess").nodeValue;
var stake = document.getElementById("stake").nodeValue;
var diceroll = roll();
if (guess === diceroll) {
balance = balance + (stake * 5);
} else {
balance = balance - stake;
}
}
//Rolling the dice
function roll() {
"use strict";
diceroll = Math.floor(Math.random() * 6 + 1);
alert(diceroll);
}
//Display balance
document.getElementById("balance").innerHTML = balance;
<!DOCTYPE html>
<html>
<head>
<title>Dice-Game: GL HF</title>
</head>
<body>
<p id="balance"></p>
<form action="#">
Enter guess:
<input type="text" id="guess">
<br>Enter stake:
<input type="number" id="stake" name="stake">
<br>
<input type="button" name="play" onclick="roll()" value="PLAY!">
<br>
</form>
<script src="Dice.js" type="text/javascript"></script>
</body>
</html>
A few things:
The way I looked at it, your while loop wasn't doing anything. The whole balance calculation wasn't even part of your roll function. I removed it altogether.
You were using .nodeValue to get your guess and stake input values, which is deprecated. It should just be .value. Those two variables were returning null.
Your balance update wasn't being called with your function either, so it wouldn't have updated on the page even if the balance was actually updating.
Your declaration of global variables was a bit redundant and unnecessary.
//declaring global variables
var balance = 1000;
//Rolling the dice
function roll() {
var guess = document.getElementById("guess").value;
var stake = document.getElementById("stake").value;
"use strict";
var diceroll = Math.floor(Math.random() * 6 + 1);
alert(diceroll);
if(guess == diceroll) {
balance = balance + (stake * 5);
} else {
balance = balance - stake;
}
//Update display balance
document.getElementById("balance").innerHTML = balance;
}
//Initial display balance
document.getElementById("balance").innerHTML = balance;
Put
//Display balance
document.getElementById("balance").innerHTML = balance;
inside of the loop.
I also don't think your while condition is correct..
Try this,
balance = parseInt(balance) - parseInt(stake);

how to use if else in 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>

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>

Categories