0

I'm trying to count the number of times certain words appear in the strings. Every time I run it I get a

uncaught TypeErro: undefined is not a function

I just actually need to count the number of times each "major" appears.

Below is my code:

for(var i = 0; i < sortedarray.length; i++)
    {
        if(sortedarray.search("Multimedia") === true)
        {
            multimedia += 1;
        }
    }
    console.log(multimedia);

Here is my csv file which is stored in a 1d array.

"NAME","MAJOR","CLASS STANDING","ENROLLMENT STATUS"
"Smith, John A","Computer Science","Senior","E"
"Johnson, Brenda B","Computer Science","Senior","E"
"Green, Daisy L","Information Technology","Senior","E"
"Wilson, Don A","Information Technology","Junior","W"
"Brown, Jack J","Multimedia","Senior","E"
"Schultz, Doug A","Network Administration","Junior","E"
"Webber, Justin","Business Administration","Senior","E"
"Alexander, Debbie B","Multimedia","Senior","E"
"St. John, Susan G","Information Technology","Junior","D"
"Finklestein, Harold W","Multimedia","Freshman","E"
4
  • If anyone needs more code I can edit it and put some more in. I'm just trying to keep it simple. Commented Sep 24, 2014 at 9:14
  • 1
    There is no such thing as Array.search. Commented Sep 24, 2014 at 9:15
  • 1
    The error you're getting effectively says Array.prototype.search is undefined. You'll have to find another way to do your search. Commented Sep 24, 2014 at 9:15
  • 1
    search is not a inbuilt function provided by Array in javascript. do you have your own implementation of it? Commented Sep 24, 2014 at 9:16

5 Answers 5

2

You need to search inside each string not the array. To only search inside the "Major" column, you can start your loop at index 1 and increment by 4 :

var multimedia = 0;
for(var i = 1; i < sortedarray.length; i += 4)
{
    if(sortedarray[i].indexOf("Multimedia") > -1)
    {
        multimedia += 1;
    }
}
console.log(multimedia);
Sign up to request clarification or add additional context in comments.

Comments

2

What you're probably trying to do is:

for(var i = 0; i < sortedarray.length; i++)
{
    if(sortedarray[i].indexOf("Multimedia") !== -1)
    {
        multimedia++;
    }
}
console.log(multimedia);

I use indexOf since search is a bit of overkill if you're not using regexes.

Also, I replaced the += 1 with ++. It's practically the same.

1 Comment

Would the downvoter care to explain what's wrong? If I made a mistake, I'd like to learn from it.
1

Here's a more straightforward solution. First you count all the words using reduce, then you can access them with dot notation (or bracket notation if you have a string or dynamic value):

var words = ["NAME","MAJOR","CLASS STANDING","ENROLLMENT STATUS"...]

var count = function(xs) {
  return xs.reduce(function(acc, x) {
    // If a word already appeared, increment count by one
    // otherwise initialize count to one
    acc[x] = ++acc[x] || 1
    return acc
  },{}) // an object to accumulate the results
}

var counted = count(words)

// dot notation
counted.Multimedia //=> 3

// bracket notation
counted['Information Technology'] //=> 3

6 Comments

Way too over-complicated for someone that's obviously learning to program.
So this stuff is over-complicated nowadays? I guess we're back to for loops, oh my.
There are a number of semi advanced concepts here, ++ on an undefined variable, boolean operations that don't return booleans, reduce functions. Still a good solution, a beginner can learn from it. It's not overly complicated, but I would not call it straight forward. Not a reason to be downvoted though.
You're using reduce, pre-increments, and that || 1. Those are functionalities / concepts that aren't handled in any beginner JS course. Beginners can only learn from this if you tell them where to find the relevant documentation, of provide a step-by-step explanation of the code.
@Cerbrus They can still learn if they do a little searching, which if they really want to learn, they will. Some inline documentation wouldn't hurt though
|
0

I don't know exactly that you need this or not. But I think its better to count each word occurrences in single loop like this:

var occurencesOfWords = {};

for(var i = 0; i < sortedarray.length; i++)
{
    var noOfOccurences = (occurencesOfWords[sortedarray[i]]==undefined? 
                          1 : ++occurencesOfWords[sortedarray[i]]);

    occurencesOfWords[sortedarray[i]] = noOfOccurences; 
}
console.log(JSON.stringify(occurencesOfWords));

So you'll get something like this in the end:

{"Multimedia":3,"XYZ":2} 

Comments

-1

.search is undefined and isn't a function on the array.

But exists on the current string you want to check ! Just select the current string in the array with sortedarray[i].

Fix your code like that:

for(var i = 0; i < sortedarray.length; i++)
{
    if(sortedarray[i].search("Multimedia") === true)
    {
        multimedia += 1;
    }
}
console.log(multimedia);

2 Comments

So this should search through the array's element and find the word I'm looking for? Thank you.
` === true` is not what you want. Have a look at the return values from String.prototype.search() (It's similar to indexOf)

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.