- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
这个程序让我很困惑。该程序的目标是计算文件中换行符的数量并在命令提示符下输出。 Learnyounode然后对文件运行他们自己的检查,看看他们的答案是否与您的答案相符。
所以我从答案开始:
var fs = require('fs');
var filename = process.argv[2];
file = fs.readFileSync(filename);
contents = file.toString();
console.log(contents.split('\n').length - 1);
learnyounode 验证该程序是否正确计算了新行数。但是当我将程序更改为以下任何一种时,它不会打印出与 learnyounode 打印出的数字相同的数字。
file = fs.readFileSync(C:/Nick/test.txt);
file = fs.readFileSync(test.txt);
难道nodejs readFileSync不能输入地址并正确读取吗?
最后,这个程序应该打印出程序中的换行符#。为什么每次我运行这个程序时,正确的程序和 learnyounode 都会打印出与换行数不同的相同数字?
例如,test.txt 中的换行数是 3。但是运行这个程序每次都会打印出不同的数字,比如 45、15、2 等。但同时,它被验证为正确的程序通过 learnyounode,因为他们的答案都匹配!怎么回事?
编辑:test.txt 是这样的
ok
testing
123
最佳答案
所以,我在本地机器上尝试了您的程序,您的程序运行良好。我不是learnyounode的专家。我只是在您提出问题后尝试过,但我想我了解它是如何工作的。因此,以下是您问题的答案:
Shouldn't nodejs readFileSync be able to input an address and read it correctly?
nodejs 的这个方法运行良好。您可以尝试打印文件的内容,您会发现没有问题。
Why does both the correct program and learnyounode print out the same number that is different from the amount of newlines everytime I run this program.
learnyounode 每次都使用不同的 filename
作为输入来运行您的程序。它通过针对同一文件运行自己的正确代码副本来验证程序的输出。
But when I change the program to any of the following, it doesn't print out the same number as learnyounode prints out.
这是因为此时,您的代码正在处理一个固定文件,而 learnyounode 在每次迭代时仍在处理不同的文件。
关于node.js - learnyounode 'My First I/O' 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20040993/
我已经查看了有关此主题的其他线程,但仍然无法弄清楚为什么我的线程不起作用。有什么想法吗? var files = process.argv.slice(2); var count = 0; var r
我读了一些相同的教程问题,但我仍然无法理解。 我用下面的代码做对了 var httpModule = require('http'); var blModule = require('bl'); v
我一直在 learnyounode 的帮助下学习 Nodejs,但我陷入了杂耍异步。我的解决方案除了问题中最困难的部分外都有效 - 它打印的结果不按顺序排列,因为某些请求先于其他请求完成。我试图在不使
我正在尝试学习 nodeschool 的 learnyounode。 This problem is the same as the previous problem (HTTP COLLECT) i
我正在尝试学习 nodeschool 的 learnyounode。 This problem is the same as the previous problem (HTTP COLLECT) i
这里似乎有很多关于这个问题的问题,但没有一个与我的问题 AFAICT 直接相关。以下是问题陈述: This problem is the same as the previous problem (H
有很多方法可以编写此代码,但我不明白为什么我的版本不起作用。 该程序应从命令行获取 2 个参数。第一个是包含文件的文件夹的路径,第二个参数是文件扩展名。输出应仅打印与第二个参数具有相同扩展名的文件。当
我一直在努力解决 learnyounode 上的 JUGGLING ASYNC 任务。据我所知,我做的事情几乎是正确的,看起来问题出在异步的某个地方,而我自己试图避免bl或其他模块。我在没有其他模块的
所以我正在解决这个名为杂耍异步的learnyounode练习,它基本上给了我3个URL并要求我按照URL输入的顺序输出URL内容。 我想出了一个递归解决方案,其中一个回调调用另一个回调,同时维护退出计
在“LearnYouNode”练习中,我对任务 9 有以下问题。在提示中提到,此练习也可以使用“asycn”或“after”包来完成。我尝试了两者,但都失败了:|(我已经完成了推荐的解决方案,没有额外
我是 Node 的新手,我正在努力通过 nodeschool.io learnyounode 交互式类(class),我遇到了 http-collect 问题。 首先是使用第三方包的实际解决方案Buf
在learnyounode上做教程,遇到如下问题。来自描述: You will be provided with three URLs as the first three command-line
所以我现在正在学习node.js,并且之前做过一些多任务处理,我的概念是异步和多任务处理有很多相似的问题,这让我想到了我的问题。 官方针对这个问题的解决方案是: var http = requir
我正在通过 learnyounode 教程学习 node.js,但一直停留在 HTTP-COLLECT 练习中。我想让我的代码在没有第三方程序帮助的情况下工作。它几乎可以正常工作,但由于某种原因,它没
当我在第 14 行运行下面的 list[i] 时,返回未定义,我不知道为什么。特别是因为我的第 10 行日志按预期返回。下面是代码, 1 var fs = require('fs');
我正在尝试使用 OSX 终端中的“Learnyounode”来学习 Node.js。当我使用 'learnyounode run programfile.js programfile.js' 运行下面
我试图在 nodeschool.io 的“learnyounode”研讨会中解决“Juggling Async”问题。我在这里看到了很多关于这个问题的问题,它不起作用,因为 url 是从循环中调用的。
我不断收到以下错误: ✗ Error connecting to localhost:20496: connect ECONNREFUSED 注意:本地主机号总是在变化。 在 cloud9.ide 上
下面是nodeschool learnyounode模块的习题5 创建一个程序,打印给定目录中的文件列表,按文件扩展名过滤。您将获得一个目录名称作为您程序的第一个参数(例如 /path/to/dir/
这个程序让我很困惑。该程序的目标是计算文件中换行符的数量并在命令提示符下输出。 Learnyounode然后对文件运行他们自己的检查,看看他们的答案是否与您的答案相符。 所以我从答案开始: var f
我是一名优秀的程序员,十分优秀!