Open Side Menu Go to the Top

05-14-2011 , 07:40 PM
There's nothing strange about it at all. It's doing what you told it to do.

Your write function is setting the value of the display to 1, 2, 3, ... 9. Every time you click a digit button it runs that and ends up with 9.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
HTML5/CSS3/advancedJavascript learning and programming logs
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
HTML5/CSS3/advancedJavascript learning and programming logs
05-14-2011 , 09:48 PM
So, here's another small solution, though it obviously isn't as clean as Thug's:

Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">

<style type="text/css">

div{
width: 100px;
height: 20px;
border-style: solid;
border-color: blue;
}

</style>


<script type="text/javascript">

function writeOne() {
document.getElementById('display').innerHTML = 1;
}
function writeTwo(){
document.getElementById('display').innerHTML = 2;
}
function writeThree(){
document.getElementById('display').innerHTML = 3;
}
function writeFour(){ 
document.getElementById('display').innerHTML = 4; 
}
function writeFive(){
document.getElementById('display').innerHTML = 5;
}
function writeSix(){
document.getElementById('display').innerHTML = 6;
}
function writeSeven(){
document.getElementById('display').innerHTML = 7;
}
function writeEight(){
document.getElementById('display').innerHTML = 8;
}
function writeNine(){
document.getElementById('display').innerHTML = 9;
}
function writeZero(){
document.getElementById('display').innerHTML = 0;
}


window.onload = function start() {
document.getElementById('one').onclick = writeOne;
document.getElementById('two').onclick = writeTwo;
document.getElementById('three').onclick = writeThree;
document.getElementById('four').onclick = writeFour;
document.getElementById('five').onclick = writeFive;
document.getElementById('six').onclick = writeSix;
document.getElementById('seven').onclick = writeSeven;
document.getElementById('eight').onclick = writeEight;
document.getElementById('nine').onclick = writeNine;
document.getElementById('zero').onclick = writeZero;
}


</script>
</head>
<body>

<div><span id="display"></span></div>

<button id="one">1</button>
<button id="two">2</button>
<button id="three">3</button>
<button id="four">4</button>
<button id="five">5</button>
<button id="six">6</button>
<button id="seven">7</button>
<button id="eight">8</button>
<button id="nine">9</button>
<button id="zero">0</button>
<button id="add">+</button>
<button id="subtract">-</button>
<button id="multiply">x</button>
<button id="divide">/</button>
<button id="equals">=</button>
<button id="clear">clear</button>
<button id="clear all">clear all</button>

</body>
</html>
Both of our solotions result in hitting <button one> and showing one, if you hit <button two> you show 2 and one is erased. I suppose this is a concatenation problem.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-14-2011 , 10:20 PM
All of your write functions need to use += instead of =. += is the same as x = x + x.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-15-2011 , 04:00 AM
Wow, I would've never, within the next 15 years, ever figured that one out.

Unfortunately, that trick didn't work in IrishThug's code.

I added an if statement to initialize and create a variable. I also added an alert function to show the new variable. For right now, it says "null", which I hope is better than it showing "display."

I placed the variable inside the functions at first, but the code seemed like it was bugging out, so I placed it in an if statement for now. Reality is that I should create an if statement that says something like:

Code:
if (n has no value){
     var n = document......
else
     create new varable (b);
I know that is totally wrong. Getting tired.

Is it legitimate to create an if statement with functions? like "if (function x){....."

So, here is what I have so far:

Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">

<style type="text/css">

div{
width: 100px;
height: 20px;
margin-left: 100px;
border-style: solid;
border-color: blue;
}

</style>


<script type="text/javascript">

function writeOne() {
document.getElementById('display').innerHTML += 1;
}
function writeTwo(){
document.getElementById('display').innerHTML += 2;
}
function writeThree(){
document.getElementById('display').innerHTML += 3;
}
function writeFour(){ 
document.getElementById('display').innerHTML += 4; 
}
function writeFive(){
document.getElementById('display').innerHTML += 5;
}
function writeSix(){
document.getElementById('display').innerHTML += 6;
}
function writeSeven(){
document.getElementById('display').innerHTML += 7;
}
function writeEight(){
document.getElementById('display').innerHTML += 8;
}
function writeNine(){
document.getElementById('display').innerHTML += 9;
}
function writeZero(){
document.getElementById('display').innerHTML += 0;
}

if (addition || subtraction || multiplication || division){
var num1 = document.getElementById('display');
}

function addition(){
}

function subtraction(){
}

function multiplication(){
}

function division(){
}

function equalTo(){
}

function backOne(){
}

function clearLine(){
}

function clearAllLines(){
}


function showResult(){
alert(num1);
}

window.onload = function start() {
document.getElementById('one').onclick = writeOne;
document.getElementById('two').onclick = writeTwo;
document.getElementById('three').onclick = writeThree;
document.getElementById('four').onclick = writeFour;
document.getElementById('five').onclick = writeFive;
document.getElementById('six').onclick = writeSix;
document.getElementById('seven').onclick = writeSeven;
document.getElementById('eight').onclick = writeEight;
document.getElementById('nine').onclick = writeNine;
document.getElementById('zero').onclick = writeZero;
document.getElementById('add').onclick = addition;
document.getElementById('subtract').onclick = subtraction;
document.getElementById('multiply').onclick = multiplication;
document.getElementById('divide').onclick = division;
document.getElementById('equals').onclick = equalTo;
document.getElementById('backSpace').onclick = backOne;
document.getElementById('clear').onclick = clearLine;
document.getElementById('clearAll').onclick = clearAllLines;
}


</script>
</head>
<body>

<div>
<script type="text/javascript">

document.write('<span id="display"></span>')

</script>
</div>


<button id="one">1</button>
<button id="two">2</button>
<button id="three">3</button>
<button id="four">4</button>
<button id="five">5</button>
<button id="six">6</button>
<button id="seven">7</button>
<button id="eight">8</button>
<button id="nine">9</button>
<button id="zero">0</button>
<button id="add">+</button>
<button id="subtract">-</button>
<button id="multiply">x</button>
<button id="divide">/</button>
<button id="equals">=</button>
<button id="backSpace">backspace</button>
<button id="clear">clear</button>
<button id="clearAll">clear all</button>
<button onclick="showResult()">result</button>

</body>
</html>
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-15-2011 , 08:30 AM
As someone else mentioned, definitely remove the repeating code and the magic numbers (numbers explicitly defined in code are generally bad practise).

Code:
function writeOne() {
document.getElementById('display').innerHTML += 1;
}
All should be removed and replaced with:

Code:
function writeNumber(num) {
document.getElementById('display').innerHTML += num;
}
I wouldn't define the onclicks in JS, I'd just add it to the tags itself as it seems neater and more compact, but this is just a preference thing I guess:

Code:
<button id="one" onclick="writeNumber(1)">1</button>
<button id="two" onclick="writeNumber(2)">2</button>
<button id="three" onclick="writeNumber(3)">3</button>
This code block:

Code:
if (addition || subtraction || multiplication || division){
var num1 = document.getElementById('display');
}
The functions being called should probably have a boolean return type for this to work
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-15-2011 , 09:51 AM
Quote:
Originally Posted by Shoe Lace
All of your write functions need to use += instead of =. += is the same as x = x + x.
I'd change the function name to append so it is more explicit what the side effect of the call is.

Quote:
Originally Posted by Gullanian
This code block:

Code:
if (addition || subtraction || multiplication || division){
var num1 = document.getElementById('display');
}
The functions being called should probably have a boolean return type for this to work
This if statement is not calling those methods as there are no parenthesis after the function names. Instead, it is just checking the reference. Since addition() has already been defined, it will always enter the if block.

Also, since you are defining the variable inside the if block, it will go out of scope when you leave the block. This means num1 will no longer have a valid reference if you try to access it later. I would also change the name of the variable. num1 is not helpful and misleading as the value isn't a number.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-15-2011 , 12:39 PM
Quote:
Originally Posted by TheIrishThug
Also, since you are defining the variable inside the if block, it will go out of scope when you leave the block. This means num1 will no longer have a valid reference if you try to access it later. I would also change the name of the variable. num1 is not helpful and misleading as the value isn't a number.
This is not true for javascript. Javascript does not have block level scope, only function level scope, so no matter where inside of a function you define a variable (inside an if statement, for statement, etc), it will always be available for the rest of the function after it's defined.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-15-2011 , 02:25 PM
Yep variables get hoisted to the top of their scope at runtime. Assuming his if statement worked (which it doesn't ) then num1's value is basically the same as:

Code:
var num1;
...when the if statement ends up being false.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-15-2011 , 03:07 PM
Ok, I didn't know that about Javascript. I haven't used it in years and I only dabbled then.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-15-2011 , 05:38 PM
Here's another way to dynamically set the onclick handler using a closure. I also renamed the button ids so we can take advantage of the for loop to set them up.

Code:
<!DOCTYPE html>
<html lang="en">
  <head>
  <meta charset="utf-8">

  <style type="text/css">
  div {
    width: 715px;
    height: 20px;
    border-style: solid;
    border-color: blue;
  }
  </style>

  <script type="text/javascript">
    function setDigit(digit) {
      return function() {
        document.getElementById('display').innerHTML += digit;
      }
    }

    window.onload = function start() {
      for(var button = 0; button < 10; button++) {
        document.getElementById('digit-' + button).onclick = setDigit(button);
      }
    }
  </script>
</head>

<body>
<div id="display"></div>

<button id="digit-1">1</button>
<button id="digit-2">2</button>
<button id="digit-3">3</button>
<button id="digit-4">4</button>
<button id="digit-5">5</button>
<button id="digit-6">6</button>
<button id="digit-7">7</button>
<button id="digit-8">8</button>
<button id="digit-9">9</button>
<button id="digit-0">0</button>
<button id="add">+</button>
<button id="subtract">-</button>
<button id="multiply">x</button>
<button id="divide">/</button>
<button id="equals">=</button>
<button id="backSpace">backspace</button>
<button id="clear">clear</button>
<button id="clearAll">clear all</button>
<button onclick="showResult()">result</button>
</body>
</html>
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-15-2011 , 06:59 PM
I'm glad you guys are having fun with this....

-----------

1000% stuck on the variable assignment. Perhaps the "showResult" button isn't a good idea after all?

I learned alot today, for example it is not okay to have:

Code:
var someArray = [];
      someArray[0] = document.getElementById('display');
All I want to do today is alert the variable and show me what is showing in the input box.

What I have tried today:

-place variable in Array

- place variable in function

- create a new function, create variable

- just lay the variable out in the open

- changed span to <input id="display>

- create a new function and create <span onchange="morefun();">

- currently working on using the "this." function, object, variable, or whatever the **** "this." is.

I think "this." may be the right direction, though I am not sure:

http://justin.harmonize.fm/index.php...ascripts-this/

http://lucassmith.name/pub/this.html
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-15-2011 , 07:33 PM
What you did works but what you want isn't the desired result. You're storing the entire element as an object into someArray[0]. someArray[0] is absolutely no different than document.getElementById('display') now.

What would you do to get the value of the display? Since we're dealing with a div, we have to use .innerHTML right? You were already doing this with the set digit function.

So if you want only the value, you might want to do:
someArray[0] = document.getElementById('display').innerHTML;

var value = document.getElementById('display').innerHTML;
...would also work (a variable called value is now storing the value, no need for an array at this point).

You will need to use an array at some point but worry about that after understanding the above first.

Last edited by Shoe Lace; 05-15-2011 at 07:40 PM.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-15-2011 , 11:35 PM
Code:
function showResult(){
    var xNum = document.getElementById('display').value;
    document.getElementById('popUp').innerHTML = xNum;
}
The only reason I can figure out why this would not be working is because using buttons to input numbers probably doesn't create a usable entity.

The result to this is "undefined." Somehow, I liked it much better when it came up "null," but after doing some research, I might not at this point.

So, I think I have to initialize a variable somewhere and set each individual function so that it initializes a variable so I can change it to a string or something. Other than space restraints, this must be why calculators limit the amount of numbers you can use in one bunch and throw an error when you type 999999999 x 999999999.

So something like: toString(a, b, c, d, e, f, g....).

I can't explain myself. I'll probably lose some more sleep tonight.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-16-2011 , 12:28 AM
You're going to have to post the whole code. There's nothing wrong with that by itself.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-16-2011 , 01:24 AM
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">

<style type="text/css">

div{
width: 100px;
height: 20px;
margin-left: 100px;
border-style: solid;
border-color: blue;
}

p{
font-size: 30em;
}

</style>


<script type="text/javascript">

function writeOne() {
document.getElementById('display').innerHTML += 1;
}
function writeTwo(){
document.getElementById('display').innerHTML += 2;
}
function writeThree(){
document.getElementById('display').innerHTML += 3;
}
function writeFour(){ 
document.getElementById('display').innerHTML += 4; 
}
function writeFive(){
document.getElementById('display').innerHTML += 5;
}
function writeSix(){
document.getElementById('display').innerHTML += 6;
}
function writeSeven(){
document.getElementById('display').innerHTML += 7;
}
function writeEight(){
document.getElementById('display').innerHTML += 8;
}
function writeNine(){
document.getElementById('display').innerHTML += 9;
}
function writeZero(){
document.getElementById('display').innerHTML += 0;
}




function addition(){
}

function subtraction(){
}

function multiplication(){
}

function division(){
}

function equalTo(){
}

function backOne(){
document.getElementById('display').innerHTML -= "\u00A0";
}

function clearLine(){
}

function clearAllLines(){
}

function showResult(){
    var xNum = document.getElementById('display').value;
    document.getElementById('popUp').innerHTML = xNum;
}

window.onload = function start() {
document.getElementById('one').onclick = writeOne;
document.getElementById('two').onclick = writeTwo;
document.getElementById('three').onclick = writeThree;
document.getElementById('four').onclick = writeFour;
document.getElementById('five').onclick = writeFive;
document.getElementById('six').onclick = writeSix;
document.getElementById('seven').onclick = writeSeven;
document.getElementById('eight').onclick = writeEight;
document.getElementById('nine').onclick = writeNine;
document.getElementById('zero').onclick = writeZero;
document.getElementById('add').onclick = addition;
document.getElementById('subtract').onclick = subtraction;
document.getElementById('multiply').onclick = multiplication;
document.getElementById('divide').onclick = division;
document.getElementById('equals').onclick = equalTo;
document.getElementById('backSpace').onclick = backOne;
document.getElementById('clear').onclick = clearLine;
document.getElementById('clearAll').onclick = clearAllLines;
}


</script>
</head>
<body>

<div><span id="display"></span></div>


<button id="one">1</button>
<button id="two">2</button>
<button id="three">3</button>
<button id="four">4</button>
<button id="five">5</button>
<button id="six">6</button>
<button id="seven">7</button>
<button id="eight">8</button>
<button id="nine">9</button>
<button id="zero">0</button>
<button id="add">+</button>
<button id="subtract">-</button>
<button id="multiply">x</button>
<button id="divide">/</button>
<button id="equals">=</button>
<button id="backSpace">backspace</button>
<button id="clear">clear</button>
<button id="clearAll">clear all</button>
<button id="popUp" onclick="showResult()">result</button>

</body>
</html>
I didn't do much significant change today.

I did discover that .value only works when dealing <input>.

The problem is, of course, that you cannot fill out <input> with buttons.

So.... I think I have to create a new object?
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-16-2011 , 02:54 PM
Actually I shouldn't have read that at 2am. Post #137 explains why it doesn't work.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-16-2011 , 08:56 PM
Maybe this will be easier if I try this question:

Is the variable tied into what is imputed into "display" initialized, ie:

does a = "display" input?

If I can do that, then:

b = new input.

Essentially, that is all I am attempting to do. It doesn't matter if the input shows up anywhere at all. I just want to know if the value is actually being saved.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-17-2011 , 01:13 AM
Code:
function showResult(){
    var xNum = document.getElementById(display.nodeValue);
    document.getElementById('popUp').innerHTML = xNum;
does nothing

Code:
var xNum = document.getElementById(display);
    document.getElementById('popUp').innerHTML = xNum;
does nothing

Code:
function showResult(){
    var xNum = document.getElementById("display");
    document.getElementById('popUp').innerHTML = xNum;
}
[objectHTMLSpanElement]

Code:
function showResult(){
    var xNum = document.getElementById("display".nodeValue);
    document.getElementById('popUp').innerHTML = xNum;
}
null
Code:
function showResult(){
    var xNum = document.getElementById('display.nodeValue');
    document.getElementById('popUp').innerHTML = xNum;
}
null

So, is hoisting the problem?

Code:
var xNum, funVar;

xNum = document.getElementById('display'); 

funVar = function showResult(){
    document.getElementById('popUp').innerHTML = xNum;
};
funVar();
does nothing.

Code:
var xNum;

xNum = document.getElementById('display'); 

function showResult(){
    document.getElementById('popUp').innerHTML = xNum;
};
null

take away semi-colon at end of function: null.

Code:
var xNum;

function showResult(){
    xNum = document.getElementById('display');
    document.getElementById('popUp').innerHTML = xNum;
}
[objectHTMLSpanElement]

Code:
function showResult(){
    var xNum;
    xNum = document.getElementById('display');
    document.getElementById('popUp').innerHTML = xNum;
};
[objectHTMLSpanElement]

Code:
function showResult(){
    var xNum = selectNodeContents(document.getElementById('display'));
    document.getElementById('popUp').innerHTML = xNum;
}
nothing

Code:
function showResult(){
    var xNum = rng.selectNodeContents(document.getElementById('display'));
    document.getElementById('popUp').innerHTML = xNum;
}
nothing

Code:
var xNum = rng.selectNodeContents(document.getElementById('display'));     

function showResult(){
    
    document.getElementById('popUp').innerHTML = xNum;
}
nothing

----------------

Do I want undefined or do I want [objectHTMLSpanElement]?

No need to ask for the whole code. It is the same exact thing I have been workin with except that I changed the innards of the function.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-17-2011 , 01:28 AM
read post 137. again if need be.

there isn't a single .InnerHTML after document.getElementById('display') in any of your tests.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-17-2011 , 01:33 AM
document.getElementById takes in the id (as a string) of an html element, and returns the html element (or null if the element is not found). innerHTML is a string property on an html element object.

Therefore, you'd want something like:

Code:
function showResult(){
    var xNum = document.getElementById('display');
    document.getElementById('popUp').innerHTML = xNum.innerHTML;
}
This is going to copy the HTML from the span element with id "display" to the button element with id "result". I'm not sure if this is exactly what you want though, since when you click the "result" button, its button text will change from "result" to whatever is in the "display" element at the time.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-17-2011 , 01:35 AM
Code:
function showResult(){
    var xNum = document.getElementById('display').innerHTML;
    document.getElementById('popUp').innerHTML = xNum;
}
#137 ftw?

Edit:
You should look at the calculator (Windows 7, not sure if it works the same in XP) and try to emulate what it does. You have an operation chain, an output and a bunch of buttons. Pretty sure the calculator is just running an eval() on the operation chain and the result of the calculation is set in the output text field.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-17-2011 , 01:42 AM
Yeah, that is sort of what I wanted. I just wanted some sort of demonstration, in Boolean terms:

If (xNum is reading the number from the display){
true
}else{
false
}

I was really just bugging out about whether or not the variable was getting stored or not. Since it did come up as [objectHTMLSpanElement], this means that the variable was in fact tied in to the input properly?

I guess I need to take a break.

I won't have any time to work on this tomorrow. Last night, I had nightmares about programming. Unreal.

Code:
function showResult(){     var xNum = document.getElementById('display').innerHTML;     document.getElementById('popUp').innerHTML = xNum; }
I imput that last night. It didn't work for some reason....

It works today.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-17-2011 , 01:53 AM
Code:
if (aNum == undefined){
    var aNum = document.getElementById("display");
    }else{
    var bNum = document.getElementById("display");
    }
}
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-17-2011 , 02:02 AM
Quote:
Originally Posted by daveT
Code:
if (aNum == undefined){
    var aNum = document.getElementById("display");
    }else{
    var bNum = document.getElementById("display");
    }
}
This code won't work, because if aNum is undefined, you can't reference aNum without a run-time exception. You want to do:

Code:
if (typeof aNum == "undefined")
You should technically use === instead of == here, but you don't need to worry about that right now, == will work just fine.

Edit: I said you can't reference aNum, but then I went ahead and referenced it in my if statement, so clearly there are cases where you can reference aNum (or any other variable name you can think of). If a variable has not been defined yet, you can only reference it by using the typeof operator (or reference it using window.aNum, but that's a longer discussion about global objects and such.) For example:

Code:
function demo(a)
{
	if (a) alert("a"); // Will run just fine, even if a value for "a" isn't passed in i.e. demo();
	if (typeof b === "undefined") alert("b"); // Will run fine
	if (b) alert("b"); // Will error
}
So the difference is even though "a" can have a value of undefined, the variable itself is defined as a parameter, so we can reference it. Since b has not been defined as a variable anywhere, we can't reference it directly unless we use the typeof operator.

Last edited by RML604; 05-17-2011 at 02:09 AM. Reason: Clarify
HTML5/CSS3/advancedJavascript learning and programming logs Quote
05-17-2011 , 03:55 AM
I found a pretty Herculean site that attempts to explain ECMA-5.

http://dmitrysoshnikov.com/ecmascrip...y-descriptors/

As you could probably tell, it created information overload for me.

-----------------------

Okay, reading up on typeof. Very interesting stuff.

Unfortunately, MozDoc would have you believe typeof is only used to figure out what you are looking at.

It almost seems like there is a division on how practical/useful typeof is. One side seems to call it worthless; others call it useful.
HTML5/CSS3/advancedJavascript learning and programming logs Quote
HTML5/CSS3/advancedJavascript learning and programming logs
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
HTML5/CSS3/advancedJavascript learning and programming logs

      
m