Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

function in javascript check repeated word not working

function numTimesWordRepeated() {
  const str =
    "The weather is good but if the weather is bad we realy need to brace for bad weather";
  const char = {};
  const arr = str.split(" ");

  for (let word of arr) {
    if (!char[word]) {
      char[word] = 1;
    } else {
      char[word]++;
    }
  }
}

console.log(numTimesWordRepeated());

>Solution :

Please provide more information about your problem. Anyway you have to return something from the function otherwise it will not console log your char map. If you don’t return anything in the function it returns undefined.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function numTimesWordRepeated() {
    const str =
        "The weather is good but if the weather is bad we realy need to brace for bad weather";
    const char = {};
    const arr = str.split(" ");

    for (let word of arr) {
        if (!char[word]) {
            char[word] = 1;
        } else {
            char[word]++;
        }
    }

    return char
}
console.log(numTimesWordRepeated());

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading