字数 JavaScript - For 循环

Words count JavaScript - For Loop

更新:我意识到在C中我初始化了单词计数器1。(我试图删除查询但我不允许):/

为了尝试学习编码,我注册了 CS50,在 PSET2 上有一个名为“可读性”的练习。我正在尝试用 JavaScript 重新创建程序。根据提供的说明,输入的输出应该是 55 个字 但我得到 54.

我的 For Loop 正在遍历数组的每个元素,如果它找到 space,它会将单词计数器加 1(我的猜测是它不计算最后一个单词,因为它以“.”结尾)

但是,我的 C 程序代码似乎工作正常。

JavaScript代码:

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程序代码

#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));
    }
    
   
}

如果两个文本中只有一个space,那么单词应该是space + 1

的个数

例如 hello world 所以只有 1 space 所以字数将是 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);

你可以在这里使用正则表达式/\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);

如果一个格式正确的句子中有 4 个空格,则有 5 个单词。示例:

“我是男孩”- 3 个空格,4 个单词

以上是一个天真的概括,但对于简单的情况,这是正确的。

因此您可以从 1 而不是 0 初始化 words。这是一个非常幼稚的解决方案,但作为开始步骤会有所帮助。

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);

编辑 :您的 C 代码用 1 初始化 words,所以这是偏移量。

这个答案有什么问题?是不是很简单

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

答案是正确的,但重点是初始化。 如果文本为空,则 'words' 应为 0。

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 textArray = Array.from(text);
let words = (textArray.length)? 1 : 0;  // set initial value for words.

textArray.forEach(e => { 
  if (e === " ") words++;
});

console.log("Words: " + words);

并且您可以使用这一行将字符串更改为数组。

// const textArray = Array.from(text);
const textArray = [ ...text ];