2

I have only been learning javascript for 2 weeks, so apologies if my question seems weird/doesn't make sense. I'm learning the basics of arrays, and to help me learn I like to practise and play around with the code but I can't seem to figure this one out.

I've created a simple function, and wanting to call upon the function to calculate the sum of variables in an array. Here is my code below:

//functions

function simpleCalc (a,b) {
    var result = a + b;
    return result;
}

//array

var myArray = [12,567];

//final calculation

var total = simpleCalc([0],[1]);

alert("The total is " + total);

Can anyone please shed any light as to how I input the numbers "12" and "567" into the function parameters? The result here as it stands outputs to "01"

Thanks

0

3 Answers 3

5

You have two options:

  1. Your option but, it is very limited to only two values.

You need to pass reference to your array elements like so (myArray[0], myArray[1])

  1. Create new function - let's call it sumValuesInArray(), pass an array and calculate all values inside an array using for loop.

See working example here:

//functions

function simpleCalc (a,b) {
    var result = a + b;
    return result;
}

//array

var myArray = [12,567];

//final calculation

var total = simpleCalc(myArray[0],myArray[1]);

//alert("The total is " + total);


// OR

function sumValuesInArray(array) {
  var total = 0;
  
  for(i = 0; i < array.length; i++) {
    var element = array[i];
    
    total += element;
  }
  
  return total;
}

console.log(sumValuesInArray(myArray));

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

3 Comments

Thanks for the quick response. Have studied this for the last hour or so and I think I've got it. So basically what you've done here is, rather than use my version which is self limiting, your version allows you to add in as many or as little array variables and the JavaScript will still run without you having to amend anymore code?
@Bayzoo That's it. You can have as many elements inside your array as you want. All will be calculated. But remember that you have to insert correct value. "22" is not the same as 22, difference is in type. "22" is typeof string, but 22 is typeof number. It is common mistake for beginners to add string into number. Example is when you take a value from an input and try to sum it. Any value you take from input will be typeof string. To convert string to number use parseInt() when you are sure your value is always intenger or parseFloat().
@Bayzoo Another solution converting string into number is unary operator +. You can google it. Please if my answer is ok to you, accept it.
1

You don't specify the array but only indexes :

var total = simpleCalc([0],[1]);

So, it passes two array objects : [0] and [1].

The concatenation of them here :

var result = a + b;

has as result the 01 String.

To pass the two first elements of myArray, try it :

var total = simpleCalc(myArray[0],myArray[1]);

2 Comments

Does first statement sends 2 unnamed array with 1 element in each ? like; first parameter is unnamed array , population :1 , first element value is 0second parameter is an unnamed array first element value is 1.
Wow it gets softer each day :D
0

You need to access the array values by index. You do this using the brackets WITH the name of the array. If you pass the brackets with numbers inside, you're creating new arrays.

It should be:

 var total = simpleCalc(myArray[0],myArray[1]);

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.