Home > Mobile >  Words count JavaScript - For Loop
Words count JavaScript - For Loop

Time:10-07

Updated: I realize that in C I initialized the word counter 1. (I've tried to delete the inquiry but I was not allowed) :/

in order to try to learn to code I signed up for CS50 and on PSET2 there's an exercise called "Readability". I'm trying to recreate the program with JavaScript. According to the instructions provided the output of input should be 55 words but I'm getting 54.

My For Loop is iterating over each element of the array and if it finds a space, it will add 1 to the words counter (my guess is that it is not counting the last word because it ends with a ".") here's a preview of JavaScript Output

However, my C program code seems to work fine. here's a preview of C Output

JavaScript Code:

let text = "It was a bright cold day in April, and the clocks were striking thirteen. Winston Smith, his chin nuzzled into his breast in an effort to escape the vile wind, slipped quickly through the glass doors of Victory Mansions, though not quickly enough to prevent a swirl of gritty dust from entering along with him."

let words = 0;
let textArray = Array.from(text);


//Words
for (let i = 0; i < textArray.length; i  ){
  if (textArray[i] == " "){
    words   ;
  }
}


console.log("Words: "   words);

C Program Code

#include <cs50.h>
#include <stdio.h>
#include <math.h>
#include <string.h>


int main(void)
{
    //Ask user for text
    string text = get_string("Text: ");
    
    //define variables
    int letters = 0;
    int words = 1;
    int sentences = 0;
    
    //loop to analyze text
    for (int i = 0; i < strlen(text); i  )
    {
        //count letters
        if ((text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z'))
        {
            letters  ;
        }
        //count words
        else if (text[i] == ' ')
        {
            words  ;
        }
        //count sentences
        else if ((text[i] == '.') || (text[i] == '?') || (text[i] == '!'))
        {
            sentences  ;
        }
    }
    
    printf("Words: %i\n", words);
    //math calculation (third rule)
    float l = 100 * ((float) letters / (float) words);
    float s = 100 * ((float) sentences / (float) words); 
    
    //printf("l: %i\n",  (int) round(l));
    //printf("s: %i\n", (int) round(s));
    
    //grade index formula
    float grade = (0.0588 * l) - (0.296 * s) - 15.8;
    if (grade >= 16)
    {
        printf("Grade 16 \n");
    }
    else if (grade < 1)
    {
        printf("Before Grade 1\n");
    }
    else 
    {
        printf("Grade %i\n", (int) round(grade));
    }
    
   
}

CodePudding user response:

If there are 4 spaces in a well-formed sentence then there are 5 words. Example:

"I am a boy" - 3 spaces, 4 words

The above is a naive generalization but for simple cases this is true.

So you can maybe initialize words from 1 instead of 0. This is a very naive solution, but will help as a starting step.

let text = "It was a bright cold day in April, and the clocks were striking thirteen. Winston Smith, his chin nuzzled into his breast in an effort to escape the vile wind, slipped quickly through the glass doors of Victory Mansions, though not quickly enough to prevent a swirl of gritty dust from entering along with him."

let words = 1;
let textArray = Array.from(text);


//Words
for (let i = 0; i < textArray.length; i  ){
  if (textArray[i] == " "){
    words   ;
  }
}


console.log("Words: "   words);

EDIT : Your C code initializes words with 1 so that is the offset.

CodePudding user response:

If there is a single space in two text then word should be number of space 1

e.g hello world so there is only 1 space so number of word will be 2

let text =
  "It was a bright cold day in April, and the clocks were striking thirteen. Winston Smith, his chin nuzzled into his breast in an effort to escape the vile wind, slipped quickly through the glass doors of Victory Mansions, though not quickly enough to prevent a swirl of gritty dust from entering along with him.";

const words = Array.from(text).filter((s) => s === " ").length   1;

console.log("Words: "   words);

You can use regex here /\s /

let text =
  "It was a bright cold day in April, and the clocks were striking thirteen. Winston Smith, his chin nuzzled into his breast in an effort to escape the vile wind, slipped quickly through the glass doors of Victory Mansions, though not quickly enough to prevent a swirl of gritty dust from entering along with him.";

const words = text.split(/\s /g).length;

console.log("Words: "   words);

CodePudding user response:

What is wrong with this answer? Isn't this simple

let text = "It was a bright cold day in April, and the clocks were striking thirteen. Winston Smith, his chin nuzzled into his breast in an effort to escape the vile wind, slipped quickly through the glass doors of Victory Mansions, though not quickly enough to prevent a swirl of gritty dust from entering along with him."

let words = 0;
// removing special characters and convert to array by space 
let wordCount = text.replace(/[^\w\s]/gi, '').split(" ").length;
console.log(wordCount) // 55

CodePudding user response:

So I checked the code, and everything seems to be correct. I also manually counted the number of spaces in the string, and it adds up to exactly 54. The problem is that the string does not have a leading space, and thus, a "word" will be skipped.

let text = "It was a bright cold day in April, and the clocks were striking thirteen. Winston Smith, his chin nuzzled into his breast in an effort to escape the vile wind, slipped quickly through the glass doors of Victory Mansions, though not quickly enough to prevent a swirl of gritty dust from entering along with him."

let words = 0;
let textArray = Array.from(text);


//Words
for (let i = 0; i < textArray.length; i  ){
  if (textArray[i] == " "){
    words   ;
  }
}


console.log("Words: "   (words   1));
  • Related