【问题标题】:Words count JavaScript - For Loop字数 JavaScript - For 循环
【发布时间】:2021-10-07 10:12:59
【问题描述】:

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

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

我的 For 循环 正在遍历数组的每个元素,如果它找到一个空格,它将给单词计数器加 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));
    }
    
   
}

【问题讨论】:

  • 您的代码只有54 空格。结果是正确的。
  • 想想This is a test这个句子有多少个空格...以及有多少个单词
  • 感谢您的回复,我似乎不明白的是它与 C 中的句子完全相同,并且代码输出 55​​ 个单词,任何关于为什么 C 得到与 JavaScript 不同的数字的问题输入相同?
  • 我用你的 C 代码得到54,而不是 55
  • 可以看到已经初始化了words=1

标签: javascript c cs50


【解决方案1】:

如果一个格式良好的句子中有 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 代码将 words 初始化为 1,这就是偏移量。

【讨论】:

  • 感谢您的 cmets!我的问题是它在 C 中是完全相同的句子,并且该代码输出 55​​ 个单词,任何关于为什么 C 在相同输入下得到的数字与 JavaScript 不同的问题?
  • 检查您在 C 代码中的初始化方式。也许添加有问题的整个代码,如果你提供一个在线 C 编译器的链接会更好
【解决方案2】:

如果两个文本中有一个空格,那么单词应该是space + 1的数字

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

【讨论】:

  • 感谢您的回复!我不熟悉正则表达式,但我会确保阅读它们。关于为什么我写它的方式跳过一个单词的任何cmets? (除了这可能不是最好的方法!大声笑)
  • @Joe 用filter更新了代码
  • 如果你不熟悉正则表达式,只需使用没有它的类似方法: text.split(' ').length
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    答案是正确的,但重点是初始化。 如果文本为空,则“单词”应为 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 ];        
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-10
      • 1970-01-01
      • 2021-10-25
      • 1970-01-01
      • 2011-07-12
      • 2012-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多