0

I'm trying to build a calculator and at this point, need to "collect" any numbers that get clicked in an array. Right now I'm unsure of where to build that array (should it be in a different function all together?). I think I'll need to use .push(), but know I need to use it on an array and am not sure where to define that. When I console.log(gridItems), clearly I get everything, but only want the numbers...

/*Calculator 

function add(n, x) {
  return n + x;
}
console.log(add(3, 8));

function subtract(n, x) {
  return n - x;
}
console.log(subtract(3, 8));

function multiply(n, x) {
  return n * x;
}
console.log(multiply(3, 8));

function divide(n, x) {
  return n / x;
}
console.log(divide(3, 8));*/

// console.log number when button is clicked

function getValue(e){
  var divValue = parseInt(e.target.textContent);
    console.log(divValue);
   }

function numberTrack() {
  
  var gridItems = document.getElementsByClassName('grid');

  for (var i = 0; i < gridItems.length; i ++) {
      gridItems[i].onclick = getValue;
  }
}

numberTrack();
#grid-container {
  width: 200px;
}

.grid {
  width: 50px;
  height: 50px;
  display: inline-block;
  text-align: center;
}

.gray {
  background-color: #ccc;
}

.pink {
  background-color: pink;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <div id="grid-container" data-value="1">
    <div class="grid gray">0</div>
    <div class ="grid pink">1</div>
    <div class="grid gray">2</div>
    <div class ="grid pink">3</div>
    <div class="grid gray">4</div>
    <div class ="grid pink">5</div>
    <div class="grid gray">6</div>
    <div class ="grid pink">7</div>
    <div class="grid gray">8</div>
    <div class ="grid pink">9</div>
  </div>
  </head>
<body>

</body>
</html>

7
  • 1
    It depends where you will use that array, will you need to use it in several other functions? If so you can just make it global. Commented Nov 18, 2015 at 18:14
  • Well, I want to push the numbers to an array for storage and will need to convert them to integers to later do math on them. I'm thinking this will be a separate function.... Commented Nov 18, 2015 at 18:16
  • What about just converting them before you store them? Something like myArray.push(+divValue) or myArray.push(parseInt(divValue))? Commented Nov 18, 2015 at 18:18
  • OP is already converting them before storing. divValue is an integer because of parseInt() Commented Nov 18, 2015 at 18:19
  • I'm a bit new to this, so could you explain (+divValue)? I don't know what the + does. Commented Nov 18, 2015 at 18:19

1 Answer 1

1

You can define it in global scope like this:

var clickedNumbers = []

function getValue(e) {
    var divValue = parseInt(e.target.textContent);
    clickedNumbers.push(divValue);
}

function numberTrack() {
    var gridItems = document.getElementsByClassName('grid');

    for (var i = 0; i < gridItems.length; i++) {
        gridItems[i].onclick = getValue;
    }
}

numberTrack();

Then push clicked number to array clickedNumbers by clickedNumbers.push(divValue)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I was playing around with something like this, but could not get it to work! I had defined my array inside getValue (not sure if that has anything to do with it) and don't think I was using .push() properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.