3

Right now I am working on this function to litterlay sum up. The array looks for example like this: var array = ['5','2','8']; I want to litterly say array[0] +1 or something like this. I look at reduce but that sums up all the values with each other, that is not what it is supposed to do. What should come out is when sum is 1, and you want the first value in the array. In this case is the first one is 5 : 5 + 1 = 6. Then 6 should come out of it and replace the place of 5 in the array.

This is an example of what I got right now.

var array = ['6','2','5'];
for(var i = 0; i < array.length; i++){
  array = array[i];
  var sum = 1;
  array = sum + array[0];
  console.log(array); 
}

5
  • 1
    please add a wanted outcome. you overwrite array and it becomes not clear, what you like to achieve. Commented Nov 7, 2017 at 14:18
  • 1
    Do you want to add 1 to each number in the array? Commented Nov 7, 2017 at 14:20
  • 1
    So what is the outcome supposed to be? Commented Nov 7, 2017 at 14:22
  • no I want to add 1 to only one value in the array and not all of them Commented Nov 7, 2017 at 14:23
  • array[index]++ ? Commented Nov 7, 2017 at 14:24

4 Answers 4

2

I want to litterly say array[0] +1 or something like this.

Looks like you want to increment all the values by 1.

Try this appoach

var array = ['6','2','5'];
array = array.map( a => String( Number(a) + 1 ) );
console.log(array);

If you want to keep them Number instead of String, then remove the wrapping with String constructor

var array = ['6','2','5'];
array = array.map( a => Number(a) + 1 );
console.log(array);

Edit - Based on comment and question update

no I want to add 1 to only one value in the array and not all of them

What should come out is when sum is 1, and you want the first value in the array.

No need for iteration then

var array = ['6','2','5'];
var sum = 1;
array[ sum - 1 ] = String( Number( array[ sum - 1 ] ) + 1 );
console.log(array);

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

Comments

1

You can use unary plus operator to convert the first element array[0] to a number and than add 1 and finally add '' to make it a string again:

var array = ['6','2','5'];
array[0] = +array[0] + 1 + '';

console.log(array);

1 Comment

OP wants no I want to add 1 to only one value in the array and not all of them
0

For a single value, you could take the value (taking an unary plus + for making a number) and increment it (make a string again), then assign it to the same index.

var array = ['6', '2', '5'];

array[0] = (+array[0] + 1).toString();

console.log(array); 

Comments

0
Array.prototype.addScalar=function(value){
   var cloneArray=[];
   this.forEach(function(entry){
     //it is weird that your array is ['1', '2'] instead of [1,2]
     //but this "if" handles it
     if ( typeof(entry)==='string') {
        cloneArray.push(parseInt(entry)+value);
     } else {
        cloneArray.push(entry+value);
     }
   });
   return cloneArray;
}

['1',2,3].addScalar(1) //[2, 3, 4]

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.