10 Replies - 591 Views - Last Post: 09 December 2012 - 09:19 AM

#1 Chenardizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-November 12

adding instead of multiplying for a noob

Posted 05 December 2012 - 01:18 PM

I am brand new to javascript and though I read a lot on these forums the response "we're not going to do your home work for you!" I am hoping for some HELP not the complete answer to my script.

That said I am trying to create a loop that takes two integers and multiplys them by using a loop to do addition.
I have already done an addition and subtraction that takes 2 user inputs and does the math.

Now I am trying to take user input and instead of doing a simple 3 * 4 loop it so it goes 3 + 3 + 3 + 3.
This is what I have so far...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<title>Multiplying</title>
	<meta http-equiv="content-type" 
		content="text/html;charset=utf-8" />
	<script type="text/javascript">
var $ = function ( id ) {
	return document.getElementById( id );
}
var display_sum = function () {
	var addNum1 = parseInt( $("addNum1").value );
	var multiNum = parseInt( $("multiNum").value );
	
	$("sum").value = "";
	
	if (isNaN(addNum1)){
		alert("First number is not a valid number");
	} else if (isNaN(multiNum)){
		alert("Second number is not a valid number");
	} else {
		var sum = 
	}
		$("sum").value = sum;
}
window.onload = function(){
	$("calculate").onclick = display_sum;
}
</script>
</head>
<body>
	<div>
		<h1>Multiplying 2 numbers</h1>
		<p>Enter the 2 numbers you wish to multiply (one in each box)</p>
		
		
		<input type="text" value=" " id="addNum1"/><br/>
		<input type="text" value=" " id="multiNum"/><br/>
		<input type="text" value="Total" id="sum"/><br/>
		<input type="button" id="calculate" value="Calculate"/><br/>
	</div>
</body>
</html>


I have edited out my loops because thus far all I have managed to do is increment the addNum1. ie I put in 3 * 4 my loops would make 7. So if anyone could just tell me which loop to use or if I need another function to do it or what I would really appreaciate any help.

Is This A Good Question/Topic? 0
  • +

Replies To: adding instead of multiplying for a noob

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7879
  • View blog
  • Posts: 13,386
  • Joined: 19-March 11

Re: adding instead of multiplying for a noob

Posted 05 December 2012 - 01:21 PM

How can we tell you what the problem is if you remove the code where the problem is?
Was This Post Helpful? 0
  • +
  • -

#3 Chenardizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-November 12

Re: adding instead of multiplying for a noob

Posted 05 December 2012 - 01:25 PM

while ( multiNum > 1 ) {
			addNum1++;
			}


I didn't include this because I deemed it rather pointless... Which is why I was asking what loop I should use...
Was This Post Helpful? 0
  • +
  • -

#4 Chenardizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-November 12

Re: adding instead of multiplying for a noob

Posted 05 December 2012 - 01:31 PM

I was thinking along the lines of:
var x =  + addNum1
do {
x
}
while (multiNum > 1)


but that wasn't working... probably because it is too simple, which is because I'm really new at this.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7879
  • View blog
  • Posts: 13,386
  • Joined: 19-March 11

Re: adding instead of multiplying for a noob

Posted 05 December 2012 - 01:42 PM

You're right, that won't work because multinum won't change. Also,
var x =  + addNum1


simply means "x gets the value of addNum, not negated". (+ is both a binary and a unary operator, and as a unary operator it simply means "the value of its operand times 1, ie, it's the complement of - in this regard)

What you want to do is to declare a variable to hold the total. Assuming your inputs are x and y, add x to total and subtract 1 from y as long as y is greater than zero. Return the value of total.

Example#1
multiply (4,2):
total = 0, x = 4, y = 2
total = 4, x = 4, y = 1
total = 8, x = 4, y = 0

Example#2
multiply (4,1):
total = 0, x = 4, y = 1
total = 4, x = 4, y = 0

Example#3
multiply (4,0):
total = 0, x = 4, y = 0


So it seems to work. Try to implement it.
Was This Post Helpful? 1
  • +
  • -

#6 Chenardizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-November 12

Re: adding instead of multiplying for a noob

Posted 05 December 2012 - 01:52 PM

Thank you. I hope I don't become a bother but I am going to be posting as I go here...
var sum = 0;
		while ( multiNum > 1 ) {
			sum += addNum1;
			multiNum--;
		}

Is this closer to being correct?
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7879
  • View blog
  • Posts: 13,386
  • Joined: 19-March 11

Re: adding instead of multiplying for a noob

Posted 05 December 2012 - 01:57 PM

Looks pretty good, aside from one problem which you'll see when you test it. :)
Was This Post Helpful? 0
  • +
  • -

#8 Chenardizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-November 12

Re: adding instead of multiplying for a noob

Posted 05 December 2012 - 02:01 PM

Oh I think I got it!
I changed the while ( multiNum > 1 ) to
while ( multiNum > 0 )
I was getting 3 * 4 = 9 and that was why.

thank you very much for your help. hopefully you'll continue to check back for my various(copious?) other problems throughout my program.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7879
  • View blog
  • Posts: 13,386
  • Joined: 19-March 11

Re: adding instead of multiplying for a noob

Posted 05 December 2012 - 02:02 PM

No problem. Carry on.
Was This Post Helpful? 0
  • +
  • -

#10 Chenardizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-November 12

Re: adding instead of multiplying for a noob

Posted 05 December 2012 - 07:29 PM

OK I was able to finish the first part on my own. Addition, subtraction, multiplication(but actually adding) and a base with an exponent (the multiplication came in here).

Now I'm in trouble again...
This time I am creating a monthly balance calculator.
This application allows the user to enter deposits and withdrawals for a bank account, and it calculates and displays the deposits and withdrawals with a running balance as well as the starting balance, total deposits, total withdrawals, and ending balance.


Could anyone tell me the logical place for me to begin?
I know I need to create an array of arrays... but one array is enough to get my head spinning.
There are also going to be a number of functions.
My guesses are
var x = function (deposits) { return document.getElementById(deposits); }
var y = function (withdrawls) { return document.getElementById(withdrawls); }
var z = function (runningBal) { return document.getElementById(runningBal); }
/*Or should these be arrays?*/


For the generated totals my starting balance is 2000... So
var runningBalance = 2000
for ( var totaldep = 0; totaldep >= 1; totaldep++) {
deposits += totaldep;
totaldep--;
}


Am I on the right track or have I derailed before leaving the station?
Was This Post Helpful? 0
  • +
  • -

#11 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,335
  • Joined: 08-June 10

Re: adding instead of multiplying for a noob

Posted 09 December 2012 - 09:19 AM

actually, I recommend you dig into Array and its methods. there are a couple of them (e.g. Array.reduce(), Array.filter()) that already do what you try to code the long way.

other than that I wouldn’t treat a withdrawal and a deposit different (they only differ in the value’s sign).

and another thing: Objects. you can’t get around with globals forever, esp. if you have to handle such delicate data as money.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1