0

I am writing a function called "countStr".

I need to return an object where each key is a word in the given string, with its value being how many times that word appeared in the given string. If the string is empty it must return an empty object.

Here's my function so far:

function countStr(str) {
   myObject = {};
  if(str.length === 0) {
    return myObject;
  } else {
    myArray = str.split(' ');
    for(var i = 0; i < myArray; i++) {
      var key = myArray[i];
      if(myObject.hasOwnProperty(key)) {
        myObject[key]++;
      } else {
        myObject[key];
      }
    }
    return myObject;
  }
}

var output = countStr('ask me lots get me lots'); 
console.log(output); // --> IT MUST OUTPUT {ask: 1, me: 2, lots: 2, get: 1}

Can you tell me how to fix it?

1
  • 1
    just missing length for myArray Commented May 23, 2017 at 7:17

3 Answers 3

2

There are small issues with your code.

  1. You need to iterate from zero to the length of the array.
  2. You need to initialize the object items with 1 in the else case.
  3. You should use local variables with the var to avoid polluting your top level namespace.

Here is the fixed version:

function countStr(str) {
  var myObject = {};
  if(str.length === 0) {
    return myObject;
  } else {
    var myArray = str.split(' ');
    for(var i = 0; i < myArray.length; i++) {
      var key = myArray[i];
      if(myObject.hasOwnProperty(key)) {
        myObject[key]++;
      } else {
        myObject[key] = 1;
      }
    }
    return myObject;
  }
}

var output = countStr('ask me lots get me lots');
console.log(output);
Sign up to request clarification or add additional context in comments.

5 Comments

Also, you should decalre myObject using var
I just added that. :) BTW the myArray declaration was also wrong.
var is not necessary
Not necessary if you want to pollute your namespace that is why I wrote should.
@Weedoze: Not necessary to run, but it is bad code to leak your local variables to the global scope.
1

You can use Array#reduce() like this

function countStr(str) {
  return str.trim().length ? str.split(' ').reduce((o,s)=>{
    o[s] = o[s] || 0;
    o[s]++;
    return o;
  }, {}) : {};
}

var output = countStr('ask me lots get me lots');
console.log(output); // --> {ask: 1, me: 2, lots: 2, get: 1}

Other wise in your code, you were missing Array.length in your for condition and the else should be like myObject[key]=1

Comments

0

function countStr(str) {
   myObject = {};
   var myArray = [];
  if(str.length === 0) {

    return myObject;
  } else {
   myArray = str.split(' ');
    for(var i = 0; i < myArray.length; i++) {
      var key = myArray[i];
      if(myObject.hasOwnProperty(key)) {
     myObject[key]++;     
      } else {     
      myObject[key]=1;
      }
    }
    return myObject;
  }
}

var output = countStr('ask me lots get me lots'); 
console.log(output);

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.