0

I'm trying to return a value acquired from the following function

function score(string,pattern)
{  
  counter=0
  for(i=0;i<string.length;i++)
  {
    if(string.toUpperCase().substr(i,pattern.length) == pattern.toUpperCase())
      counter++
  }   
  return counter  
}

into the following array

result=[]

for(i=0;i<web.length;i++){
  result[result.length]=score("Coooool!","OO")
  }

such that result=4,4,4 currently, result=4

Any help is appreciated

6
  • try pushing to the array instead. Commented Nov 17, 2016 at 12:16
  • As @KevinKloet commented: use .push(): result.push(score("what","ever")); Commented Nov 17, 2016 at 12:18
  • Can you explain what are you trying to achieve? Commented Nov 17, 2016 at 12:20
  • You should also consider removing the score function from the question; it is not relevant at all. [Correction: it seems to matter that the value doesn't change, I didn't edit the question though.] Commented Nov 17, 2016 at 12:21
  • I have rolled back your edit as definition of score is required Commented Nov 17, 2016 at 12:23

2 Answers 2

2

Issue is, when you define variables without var, they become part of global scope.

Your Code

function score(string, pattern) {
  counter = 0
  for (i = 0; i < string.length; i++) {
    if (string.toUpperCase().substr(i, pattern.length) == pattern.toUpperCase())
      counter++
  }
  return counter
}

result = []

for (i = 0; i < 4; i++) {
  console.log("Before call: ", i)
  result[result.length] = score("Coooool!", "OO")
  console.log("After call: ", i)
}

console.log(result)

Resolved Code

function score(string, pattern) {
  var counter = 0
  for (var i = 0; i < string.length; i++) {
    if (string.toUpperCase().substr(i, pattern.length) == pattern.toUpperCase())
      counter++
  }
  return counter
}

var result = []

for (var i = 0; i < 4; i++) {
  result[result.length] = score("Coooool!", "OO")
}

console.log(result)

Also as rightly pointed out by kevin kloet, you should use .push instead of result[result.length]

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

2 Comments

In the resolved code, if i remove the var before i in the score function, I'm right back at [4]. Why is this happening?
It will update i in external for as well. variables are hoisted. So var i will move at top and for would look like for(i=0; i<4; i++)
0

You are indexing your array based on the length rather than the counter. So change it to result[i]=... like below.

result=[]

for(i=0;i<web.length;i++){
    result[i]=score("Coooool!","OO")
}

You could also push to the array:

result=[]

for(i=0;i<web.length;i++){
    result.push(score("Coooool!","OO"))
}

4 Comments

Thanks for the answer, however both solutions still makes result=4
what is the length of web when you do console.log(web.length)?
What does web look like?
result=[0,0,0] for(i=0;i<3;i++){ result[i]=score("Coooool!","OO") } seems to return 4,0,0, not 4,4,4

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.