Computer Forums (sqrt) √ function: Javascript/HTML help

 11-20-2014, 10:16 PM #1 Solid State Member   Join Date: Sep 2014 Location: US Posts: 13 (sqrt) √ function: Javascript/HTML help I developed a calculator but cannot get the (sqrt) "√" function key to work properly. Can someone help me with this? Here is the calculator..........file:///C:/Users/twhecht68/Desktop/Calculator/calculator.html Calculator

Todd's Calculator

__________________ __________________
 11-21-2014, 01:30 PM #2 Site Team     Join Date: Mar 2004 Posts: 8,096 Re: (sqrt) √ function: Javascript/HTML help in order to use the root function you must pass it a value. it doesn't matter if that value is a static number res = math.sqrt(9) or a variable var x = 3; var y = 3; var z = x*y; var res = math.sqrt(z); many more of your function names won't be allowed either. ( . * / -) __________________ __________________ I didn’t fight my way to the top of the food chain to be a vegetarian… Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns. "The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
 11-22-2014, 01:11 PM #3 Solid State Member   Join Date: Sep 2014 Location: US Posts: 13 Re: (sqrt) √ function: Javascript/HTML help Root, I can attach a value and variable to my code which will make the sqrt function work. However, the function works only outside of the calculator display and directly onto the box. Here are the two lines of code.

The new problem I am having is putting the new code below into this line of code above.

Here is the javascript code I used to make it work. function init() { var myButton = document.getElementById("btnSubmit"); myButton.onclick = displaySquare; } onload = init; function displaySquare() { var inputVal = parseInt(document.getElementById("txtNumber").valu e); var result = document.getElementById("result"); var resultMessage = ""; if (isNaN(inputVal)) { alert("Please, enter a number"); } else { var squareVal = calculateSquare(inputVal); if (squareVal) { resultMessage = "Square root of " + inputVal + " is " + squareVal; } else { resultMessage = "Sorry, an error occurred"; } } result.innerHTML = resultMessage; } function calculateSquare(input) { var squareVal = Math.sqrt(input); return squareVal; } __________________
 11-24-2014, 05:21 AM #4 Site Team     Join Date: Mar 2004 Posts: 8,096 Re: (sqrt) √ function: Javascript/HTML help I think two things at the moment. First, posting snippets of code isn't really that helpful, without an explanation. second, I don't think you've thought about how a calculator works internally. Here is are a couple of simple question about your calculator. How are you entering the number 11? (by pressing the 1 button twice?) Are you displaying this in a "screen" like a normal calculator? What it really comes down to is. In my opinion, the biggest problem is the way that you're thinking about the problem. -if you had a clear picture of how the "machine" works, then it'd be a lot easier. Perhaps you should have a flow chart to help organise things? by the time you press the square root button, you should already have the variable saved in a place (as a global variable) that you can just use. If you want I can take you through step by step creating a calculator in javascript, but, that may involve you modifying your code extensively to fit in with the example. __________________ I didn’t fight my way to the top of the food chain to be a vegetarian… Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns. "The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
 11-24-2014, 08:37 AM #5 Fully Optimized     Join Date: Nov 2014 Location: United States Posts: 1,653 Re: (sqrt) √ function: Javascript/HTML help It appears that the calculator that he is trying to use/create is web based. But yes, it would be nice to get answers. __________________ Will have a youtube channel up soon. Link will be here if I remember.
 11-24-2014, 06:18 PM #6 Solid State Member   Join Date: Sep 2014 Location: US Posts: 13 Re: (sqrt) √ function: Javascript/HTML help Root, Good points. I took bits and parts of code and learned how to code from online tutorials to build the skeleton in HTML5. I then mapped out on paper which buttons, display, and structure I wanted to include. Then I learned CSS and figured out how to add the colors. Next, I created functions I learned from tutorials, websites, and books to make it work. I thought it would be easier ----- but logic, variables, and functions has its limits---or the beginner programmer has its limits. I am in over my head. You are right I have to know the internals of the calculator. A flow chart would be helpful. In HTML5 I broke it down by rows. In Javascript I created functions and assigned specific variables to make the buttons and math functions work properly. I have tried to develop a function, used other programmers functions, learned from tutorials and more books---on how to get that sqrt rt to actually work correctly in the display window. The calculator looks like a basic calculator with a gray display and if I type in "11" I have to type "1" "1" separately. The sqrt root is the most challenging aspect of this calculator. There is no specific segment that will work. I have tried various functions () and math sqrt(9) and simpler functions, tested it over and over but---the sqrt either either works off the skeleton or it won't display correctly. __________________
 11-25-2014, 05:48 AM #7 Fully Optimized     Join Date: Nov 2014 Location: United States Posts: 1,653 Re: (sqrt) √ function: Javascript/HTML help Well, you have my best wishes for trying to take on such a difficult project. This really is how you learn. At best, you have already learned a lot, even through failure. It will not be clear yet, but think back to where you started, and you will see what I mean. __________________ Will have a youtube channel up soon. Link will be here if I remember.

I've included some comments in the html these are denoted by: especially whilst you;re learning you should use comments to remind yourself why you have done something, or why you chose not to do something some other way that might look obvious but just not work. it's fair to say that "in real life" you may have chosen a different button order, and a different layout, you'd have used CSS to make all the buttons the same size and control the layout. you'd have made them different colours. but, first lets get a working calculator and make it pretty later on. (I'm breaking this up into lots of different posts. and they'll be posted probably over a few days, if you want me to go back over anything, or want something explained in some more details etc then let me know.) __________________ I didn’t fight my way to the top of the food chain to be a vegetarian… Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns. "The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
 11-26-2014, 03:17 AM #9 Site Team     Join Date: Mar 2004 Posts: 8,096 Re: (sqrt) √ function: Javascript/HTML help So, I said that you need to think about how a calculator works, in some ways its pretty simple, the screen starts off with a zero on it. when you press a button a value gets displayed on the screen. so lets get that working first of all. since we're looking at how to get numbers input into the calculator it makes sense to call the function input, and to pass the data that we wish to "input". what will happen is that as a button is pressed the calculator will receive that value, store it in a register. and then display that register. Code: jsCalc

0

__________________ I didn’t fight my way to the top of the food chain to be a vegetarian… Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns. "The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
 11-26-2014, 03:43 AM #10 Site Team     Join Date: Mar 2004 Posts: 8,096 Re: (sqrt) √ function: Javascript/HTML help you'll notice that if you press 1, a 1 appears in the screen. then if you press 2 the 1 disappears and is replaced by a 2. there are a few improvements that can be made to the code to make it a bit more readable first. Code: var register_a; register_a = 0; doesn't need to be that long, it could just be Code: var register_a = 0; Then there is the issue that rather than numbers being concatenated and displayed side by side, they are just replaced. so let's look at some code to catenate data. there are a couple of ways of doing this is Java one is to use the concat function, another is to use the + sign, so Code: string1 + string2 => string1string2 of course in numbers it's not that simple Code: 2 + 2 will display 4, so putting an empty text part before it forces it to construct the numbers like a sentence. Code: register_a = "" + register_a + num; So the complete code now becomes Code: jsCalc

0

__________________ __________________ I didn’t fight my way to the top of the food chain to be a vegetarian… Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns. "The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."