0

I am trying to do a addition method in a javascript variable, below is my code:

<html>
<body>
  <p id="demo"></p>
  <p id="demo1"></p>
  <p id="demo2"></p>
  <script>
  var juice = [];
  var water = [];
  var fruits = [2, 5, 7, 10,15, 25,28,34,38,45,49,52,55,57,59];
  for(var i =0;i < fruits.length;i++){
        var today = new Date();
        var numberOfDaysToAdd = fruits[i] ;
        today.setDate(today.getDate() + numberOfDaysToAdd); 
        var dd = today.getDate();
        var mm = today.getMonth()+1; //January is 0!
        var yyyy = today.getFullYear();
        if(dd<10) {
           dd='0'+dd
        } 
        if(mm<10) {
           mm='0'+mm
        } 
        today = mm+'/'+dd+'/'+yyyy+' $ ';

     juice[i] = today;
    for( var j=0; j<=juice.length; j++)
   {
        water[j] += juice[j][4];

   }

  }
  document.getElementById("demo").innerHTML = juice.length;
  document.getElementById("demo1").innerHTML = juice;
  document.getElementById("demo2").innerHTML = water;
  </script>    
</body>
</body>
</html>

I want to do addition 4 to the above water variable and print the output, I tried coding as above, but I am unable to achieve the functionality .

5
  • What are you trying to achieve? juice is a one dimension array and in second for loop you are trying to access index juice[0][4], juice[1][4]. Commented Apr 25, 2016 at 8:55
  • @AfshanShujat: I want to do addition for the juice array elements with 4, for example: var a=[1,3,5,6,9,12,16, 18], I want to add 4 to that array elements, a[0]=1 by adding 4 to it, now a[0]=5 and my remaining output should be like a[5,7,9,10,13,16,20, 22] Commented Apr 25, 2016 at 9:20
  • You can do this by simply running each loop or for loop for array a and can add a[i] +=a[i]+4; Commented Apr 25, 2016 at 9:27
  • from this: today = mm+'/'+dd+'/'+yyyy+' $ '; juice[i] = today; looks like juice is an array of strings, or I'm missing something? Commented Apr 25, 2016 at 9:27
  • Do u want to add 4 more days to juice like juice = today+4 days? Commented Apr 25, 2016 at 9:32

2 Answers 2

2

Try using Array.prototype.reduce as below

var a = [1,3,5,6,9,12,16, 18];
var sum = a.reduce(function (prev, current) {
            return prev + current
          }, 0);

document.querySelector('#content').innerHTML = sum;
<div id='content'> </div>

If you need to add 4 to each use Array.prototype.map as below:

    var a = [1,3,5,6,9,12,16, 18];
    var sum = a.map(function (val) {
                return val + 4;
              });

    document.querySelector('#content').innerHTML = sum;
<div id='content'></div>

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

1 Comment

thanks for your comment, but my requirement is different from your answer, I want to add 4 for each array element for example: var a=[1,3,5,6,9,12,16, 18], I want to add 4 to that array elements and my output should be like a[5,7,9,10,13,16,20, 22]
0

The statement water[j] += juice[j][4]; in the second for loop won't work because you're using the += operator with array elements that have never been defined to begin with. For example if I have:

var x = [];
x[0] += 1;

this results in NaN. If I do it with strings like so:

var x = [];
x[0] += 'a';

it results in 'undefineda'. So that needs to be changed.

Secondly, what kind of integer addition you are trying to do? juice is an array of strings such as '04-24-2016' based on your code. Do you mean to add 4 days to whatever date is stored in juice[j]?

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.