Javascript math not working, better way to perform operations? - javascript

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

Related

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

What am i doing wrong with this JavaScript code?

I am trying to make a javascript program that solves the Pythagorean theorem when you give it inputs . But for some reason its not working. Here is the code: `
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' href='style.css'/>
<script>
var a = document.getElementById("a").value;
var b = document.getElementById("b").value;
function myFunction() {
var one = Math.pow(a, 2);
var two = Math.pow(b, 2);
var xone = (one+two);
var c = Math.sqrt(xone);
alert("c="+c);
}
</script>
</head>
<body>
<strong>A</strong>
<input type="text" id="a"></input> <br>
<strong>B</strong>
<input type="text" id="b"></input><br>
<button onclick="myFunction()">Enter</button>
</body>
</html>
First you need to move your gathering of the data inside of the function. Otherwise you will never have a value for the inputs.
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' href='style.css'/>
<script>
function myFunction() {
//moved two lines below so they are inside the function declaration.
var a = document.getElementById("a").value;
var b = document.getElementById("b").value;
var one = Math.pow(a, 2);
var two = Math.pow(b, 2);
var xone = (one+two);
var c = Math.sqrt(xone);
alert("c="+c);
}
</script>
</head>
<body>
<strong>A</strong>
<input type="text" id="a"></input> <br>
<strong>B</strong>
<input type="text" id="b"></input><br>
<button onclick="myFunction()">Enter</button>
</body>
</html>
Just replace the script section with the following one:
<script>
function myFunction() {
var a = document.getElementById("a").value;
var b = document.getElementById("b").value;
var one = Math.pow(a, 2);
var two = Math.pow(b, 2);
var xone = (one+two);
var c = Math.sqrt(xone);
alert("c="+c);
}
</script>
All you had to do is to move the getElementById lines insied the function :)

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>

Javascript 'NaN' on calculate

I am new to javascript and trying to make a basic BMI calculator.
But I must be doing something wrong as it displays 'NaN' when I click on calculate.
Any ideas out there?
Thanks
<!DOCTYPE html>
<html>
<body>
<form>
Height: <input id="h" type="text" name="height"><br>
Weight: <input id="w" type="text" name="weight">
</form>
<button onclick="myFunction()">Calculate</button>
<p id="bmi"></p>
<script>
function myFunction()
{
var h;
var w;
var x=Math.round(w/(h*h));
var demoP=document.getElementById("bmi")
demoP.innerHTML="Your BMI is: " + x;
}
</script>
</body>
</html>
Try this:
function myFunction() {
var h = document.getElementById("h").value;
var w = document.getElementById("w").value;
var demoP = document.getElementById("bmi");
var x;
//checking are h and w numbers
if (isNaN(h) || isNaN(w)) {
x = 'Bad value';
} else {
x = Math.round(w/(h*h));
}
demoP.innerHTML="Your BMI is: " + x;
}
var h = document.getElementById("h").value;
var w = document.getElementById("w").value;
Please initialize value of h and w using above code.acknowledge user2110655
some changes:
var h = document.getElementById("h").value;
var w = document.getElementById("w").value;
Try this,
Added
var h = document.getElementById("h").value;
var w = document.getElementById("w").value;
Jaavscript:
function myFunction()
{
var h = document.getElementById("h").value;
var w = document.getElementById("w").value;
if((h!="" && isNaN(h)) && (w!="" && isNaN(w) ){
var x=Math.round(w/(h*h));
var demoP=document.getElementById("bmi");
demoP.innerHTML="Your BMI is: " + x;
}
}
var h;
var w;
var x=Math.round(w/(h*h));
h and w have not been initialized. hence X is not a number.
Try these vars :
var h = parseInt(document.getElementById('h').value());
var w = parseInt(document.getElementById('w').value());

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