0

I'm trying to add the random number to the array which is assigned to a function that keeps adding the numbers together.

function getMiNumber(number){
    var value = Math.floor(Math.random() * 10) + 1 ;
    value.push(value.value);

    var myArray = someCalc([0]);
    console.log(myArray);
    function showMe(val) {
      var presentMe = document.getElementById('someId');
      presentMe.innerHTML = val;
    }
    showMe(myArray);

            function someCalc(list) {
      var total = 0;
      for(var i = 0; i < list.length; i++){
        total += list[i];
      }
      return total;
    }

}
2
  • well ... the argument number is never used in the function, value.value is undefined, as a Number does not have a property called value, and value doesn't have .push method, as it is not an Array ... that's just lines 1, 2 and 3 of your code - line 3 would produce an error in developer tools console ... Commented Jan 2, 2016 at 2:58
  • and line 5 will always result in the myArray being the value 0 Commented Jan 2, 2016 at 3:02

1 Answer 1

1

UPDATE

Reread the question and determined that a cumulative total is desired so this demo will:

  • Generate a random number 1 to 10.
    • Display that number.
  • Add that number to an array.
    • Display that array.
  • Add all of the array's elements.
    • Display that total.

<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <title>34562147</title>
  <style>
    html,
    body {
      font: small-caps 400 16px/1.4 'Source Code Pro';
    }
    fieldset {
      border: 2px inset grey;
      border-radius: 10px;
    }
    legend {
      font-size: 1.3rem;
    }
  </style>
</head>

<body>
  <fieldset>
    <legend>Random Array Total</legend>
    <input id="btn1" type="button" onclick="rand()" value="Random" />
    <br/>
    <label for="out0">Next:
      <output id="out0"></output>
    </label>
    <br/>
    <label for="out1">Array:
      <output id="out1"></output>
    </label>
    <br/>
    <label for="out2">Total:
      <output id="out2"></output>
    </label>
  </fieldset>
  <script>
    var xArray = [];

    function rand() {
      var ran1 = Math.floor(Math.random() * 10) + 1;
      var out0 = document.getElementById('out0');
      var out1 = document.getElementById('out1');
      var out2 = document.getElementById('out2');
      out0.value = ran1;
      xArray.push(ran1);
      out1.value = xArray;
      out2.value = calc(xArray);
    }

    function calc(xArray) {
      var total = 0;
      for (var i = 0; i < xArray.length; i++) {
        total += xArray[i];
      }
      return total;
    }
  </script>
</body>

</html>



OLD

The array is pre-determined in this demo var xArray = [23, 8, 90, 7];

function randomNumber(xArray){
    var value = Math.floor(Math.random() * 10) + 1 ;
    xArray.push(value);
		return xArray;
}
    var xArray = [23, 8, 90, 7];
    var total = calc(randomNumber(xArray));
    console.log(total);
    function output(total) {
      var out1 = document.getElementById('out1');
      out1.value = total;
    }
    

 function calc(list) {
      var total = 0;
      for(var i = 0; i < list.length; i++){
        total += list[i];
      }
      return total;
    }

output(total);
<output id="out1"></output>

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

Comments

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.