Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com JavaScript algorithm not working

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 01-17-2013, 02:11 AM PM User | #1 Taro Regular Coder     Join Date: Oct 2011 Location: Geraldton, Ontario Posts: 155 Thanks: 1 Thanked 1 Time in 1 Post JavaScript algorithm not working Hello, I've been working on a basic script that determines a net value based on four user-input values. After all the values have been stored, the operators do their work in calculating the final solution. If you notice below, the values in the comments are what the literal version of the equation is made up of; (x2-x1) + (x3-x2) + (x4-x3)= y. In reference, if the snippet `n-j` and the other variables were converse, the answers came out to be opposite from each other, as in the absolute or negativity value. PHP Code: ``` function jah(){var PH = document.getElementById('myDiv');if (PH)PH.innerHTML = "?";}//end of jah functionfunction fourFunction(){var n=prompt("What is the first value?","0");n=parseFloat(n);if(isNaN(n)){alert("This input cannot be parsed into a number.");return;//x1}elsevar j=prompt("What is the second value?","0");j=parseFloat(j);if(isNaN(j)){alert("This input cannot be parsed into a number.");return;//x2}elsevar k=prompt("What is the third value?","0");k=parseFloat(k);if(isNaN(k)){alert("This input cannot be parsed into a number.");return;//x3}elsevar v=prompt("What is the fourth value?","0");v=parseFloat(v);if(isNaN(v)){alert("This input cannot be parsed into a number.");return;//x4}elsevar u = n-jvar q = j-kvar s = k-vvar b = u+q+svar MD = document.getElementById('myDiv');if (MD)MD.innerHTML = b;}//end of fourFunction()

Net Value Calculator 1.0

This script calculates the net value given a range of respective integers.

Create ListReset?
``` I think that the problem lies where the part of the code has the instantiated varaibles, along with their operators. I tried switching them out, reconfigured them, and even had to reference back, but came to no avail. With respect to the algorithm, 1.94, 1.95, 1.95, 1.95 works, but 0, 4, 0, 0 does not work because the solution came out to be 0 for some reason. __________________ Element ID Webs Support Helper Your friendly neighborhood Taroman. Last edited by Taro; 01-17-2013 at 02:14 AM..
 01-17-2013, 02:48 AM PM User | #2 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,955 Thanks: 75 Thanked 4,308 Times in 4,275 Posts Ummm...I think you need to go back and take pre-algebra again. If you do: x1 = 0; x2 = 4; x3 = 0; x4 = 0; Then you do: y = (x2-x1) + (x3-x2) + (x4-x3); you will be doing: y = (4 - 0) + (0 - 4) + (0 - 0) which is y = (4) + (-4) + 0 which is, correctly: y = 0 ********* In any case, just going one step further into elementary algebra, why not SIMPLIFY this equation: y = (x2-x1) + (x3-x2) + (x4-x3); which becomes: y = x2-x1+x3-x2+x4-x3; which becomes y = x2-x2 + x3-x3 - x1 + x4 which becomes y = 0 + 0 - x1 + x4 which becomes y = x4 - x1 In other words, the values entered for x2 and x3 *DO NOT MATTER*. The end result will only depend on x1 and x4. Me thinks you have much much bigger problems than just your JavaScript. Which is actually working correctly. __________________ An optimist sees the glass as half full. A pessimist sees the glass as half empty. A realist drinks it no matter how much there is.

 Bookmarks

 Tags algorith, equation, integer

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 01:16 AM.