JavaScript Javascript: Inserting data from an array into function parameter javascript inserting data into html table,javascript append data to

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

Answer: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]);
Answer:2

Good morning everyone, I have been working on this all night and I need some assistance. I am trying to use JavaScript to dynamically change a log in button's color when I log in to my site. It seems ...

Good morning everyone, I have been working on this all night and I need some assistance. I am trying to use JavaScript to dynamically change a log in button's color when I log in to my site. It seems ...

  1. html javascript dynamic table
  2. html javascript dynamic list
  3. html javascript dynamic form
  4. html javascript dynamic content
  5. html javascript dynamic drop down list
  6. html javascript dynamic select
  7. html javascript dynamic id
  8. html javascript dynamic text
  9. html javascript dynamic drop down
  10. html javascript dynamic variable
  11. html javascript dynamic website
  12. html javascript dynamic webpage
  13. html javascript dynamic button
  14. html javascript dynamic text field
  15. html dynamic javascript loading
  16. javascript html dynamic
  17. javascript dynamic html elements
  18. javascript dynamic html template
  19. dynamic html javascript tutorial
  20. javascript dynamic html event

I think I'm over-complicating things for a beginner but all I wanted to do was add addEventListener at once to four of my buttons. Then pass on that event, to a new function which checks which button ...

I think I'm over-complicating things for a beginner but all I wanted to do was add addEventListener at once to four of my buttons. Then pass on that event, to a new function which checks which button ...

  1. audio.pause not working
  2. audio_play_sound not working gamemaker
  3. javascript audio pause not working
  4. html5 audio pause not working

I would like to show value in select option which value was query from database. And this value should be use in another functions, not to show only. This is html script with JQM. <div data-role=...

I would like to show value in select option which value was query from database. And this value should be use in another functions, not to show only. This is html script with JQM. <div data-role=...

  1. show that value of g decreases with height

I am using ReactJs to develop my application. I am trying to submit an input text when Enter is pressed by handling the onKeyPress event. It detects other inputs, but not enter. I have tried different ...

I am using ReactJs to develop my application. I am trying to submit an input text when Enter is pressed by handling the onKeyPress event. It detects other inputs, but not enter. I have tried different ...

  1. detect magic 5e
  2. detect language
  3. detect thoughts 5e
  4. detect magic
  5. detect synonym
  6. detect definition
  7. detect magic pathfinder
  8. detect good and evil 5e
  9. detect thoughts
  10. detect meaning
  11. detect font
  12. detect life skyrim
  13. detect evil pathfinder
  14. detect poison and disease
  15. detect cycle in directed graph
  16. detect pokemon
  17. detect vs protect
  18. detect hidden camera
  19. detect magic 3.5
  20. detect font from image