Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

Adding percentage button to javascript calculator

General Tech Learning Aids/Tools
Max. 2000 characters
Replies

usr_profile.png
Yasmin Mirza

User

( 6 months ago )

 

I am working on a calculator program to practice and aid in learning javascript. I have this so far:

 

<!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>
<meta content="text/html; charset=utf-8" http-equiv="Content-type" />
<title>Calculator</title>
<h1><center>On-Line Calculator</center></h1>
<script LANGUAGE="Javascript">
function addChar(input, character) {
	if(input.value == null || input.value == "0")
		input.value = character
	else
		input.value += character
}

function sqrt(form) {
	form.display.value = Math.sqrt(form.display.value);
}

function changeSign(input) {
	if(input.value.substring(0, 1) == "-")
		input.value = input.value.substring(1, input.value.length)
	else
		input.value = "-" + input.value
}

function compute(form) {
	form.display.value = eval(form.display.value)
}

function checkNum(str) {
	for (var i = 0; i < str.length; i++) {
		var ch = str.substring(i, i+1)
		if (ch < "0" || ch > "9") {
			if (ch != "/" && ch 
								 								 
							usr_profile.png
							
Kajal Gaur

User

( 6 months ago )

 

The calculator above doesn't seem to work for me.
However, I can make a couple of suggestions that think will help you a long the way.

A) Please don't take this the wrong way. Part of programming is resolving the issues that you are having -- that is the fun of it. Gets more fun when your under pressure of deadlines.

B) I would recommend using jQuery. You will inherently learn javascript.

C) Finally, use div tags to write the validated key press to read from that separating the values by operators.

eg:

calculation=$('#output_div_id').val();
calculation_parts = calculation.split('+');
answer = parseInt(calculation_part[0]) + parseInt(calculation_part[1]).

$('#output_div_id').html($('#output_div_id').html()+ "=" + answer);

Hope that helps - good luck with learning java

what's your interest