Adding percentage button to javascript calculator

General Tech Learning Aids/Tools 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years 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>Calculatortitle>
<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 
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years 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:

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

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

Hope that helps - good luck with learning java


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.