user1434249
user1434249

Reputation: 21

JavaScript - can't pass parameters in function

I am trying to run a function in my JavaScript code. I'm trying to create two arrays, one with arabic words and one with the translated words in english, where corresponding words in each array have the same index. The purpose of the function is so that I can add an arabic word and its translation simultaneously.

The function doesn't run when I call it, and I have determined that the fact I am passing parameters in the function is what's causing it to not run. Why does this happen and how can I get the function to run? The script is in the <body> of the HTML.

This is my code:

var arabic = [];
var english = [];    
function addToArrays(arabic, english) {
    arabic.push(arabic);
    english.push(english);
}
addToArrays("string1", "string2");

Upvotes: 1

Views: 2312

Answers (1)

user672118
user672118

Reputation:

Your parameter names are override your arrays. So what you need to do is rename your parameters. Something like this should work.

var arabic = [];
var english = [];

function addToArrays(a, e) {
  arabic.push(a);
  english.push(e);
}

addToArrays("string1", "string2");

Upvotes: 5

Related Questions