Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Javascript triangle project

Name: Conserned Citizen 2012-10-27 22:00

So I'm trying to write a JS that takes the sides of triangles and determines if they're real or not. It keeps fucking up though and I have NO idea why.
Code:
//Gather input
var side1 = prompt("Input the length of side A(Not the longest).");
var side2 = prompt("Input the length of side B(Not the longest).");
var side3 = prompt("Input the length of side C(Longest side).");
//Create triangle object
var triangle = new Object();
//Find out if the triangle is real
real = function() {
if (side1 + side2 <= side3 || side2 + side1 <= side3) {
triangle.real = "not real";
}
else{
triangle.real = "real";
}
};
//Call functions and display answer
real();
alert("The triangle is " + triangle.real + ".");

Name: Anonymous 2012-11-13 20:01

>>1
Hi OP, the problem is that prompt() returns a string, but your numerical calculations (side1 + side2 <= side3 and so on) require numbers.

To convert the user input to numbers, we need JavaScript's built-in parseFloat function. Try wrapping your prompts with parseFloats, like so:

var side1 = parseFloat(prompt("Input the length of side A(Not the longest)."));
var side2 = parseFloat(prompt("Input the length of side B(Not the longest).");
var side3 = parseFloat(prompt("Input the length of side C(Longest side)."));


Hope that helps! :)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List