如何使用 Javascript 解决编码难题?
有一个名为 Gild.com 的网站,其中有不同的编码难题/挑战供用户完成。它们可以用多种语言完成,包括 Javascript。我有兴趣用 Javascript 解决这些难题,但我不确定以下几点:
- 我应该如何访问应该作为参数传递的输入文件?
- 我应该如何输出结果?
我对 Javascript 的理解是,它是从 HTML 页面内运行的,并且输出实际上只是以在 HTML 中放置值、修改 DOM 等的形式。因此,我不清楚如何使用 Javascript来解决这些类型的问题。以前使用过 Gild 或对我的问题有一些见解的人可以建议如何继续吗?
问题的一个例子是:给定的输入文件包含一个正整数,找到所有小于该整数的素数之和并将其输出。
编辑:下面的一些解决方案涉及使用外部资源,但在 Gild 上,我应该将我的解决方案放入他们的编辑器中,然后以这种方式提交,如下图所示:
换句话说,我认为我的解决方案无法访问 Node.js 或其他外部资源。
编辑:以下是我发现的一些有趣的文章,我认为它们可以回答我的问题:
There is a website called Gild.com that has different coding puzzles/challenges for users to do. They can be completed in wide array of languages including Javascript. I am interested in solving these puzzles in Javascript, but I am unsure of the following:
- How am I supposed to access the input file which is supposed to be passed as an argument?
- How am I supposed to output the result?
My understanding of Javascript is that it is run from within an HTML page and that output really is only in the form of placing values in the HTML, modifying the DOM, etc. For that reason it is not clear to me how Javascript can be used for solving these types of problems. Can someone who has used Gild before or has some insights into my question suggest how to proceed?
An example of a problem would be: the given input file contains a positive integer, find the sum of all prime numbers smaller than that integer and output it.
EDIT: Some of the solutions below involve using external resources, but on Gild, I am supposed to put my solution in their editor and then submit it that way, like the following picture shows:
In other words, I don't think my solution can have access to Node.js or other external resources.
Edit: Here are some interesting articles that I have found that I think are the answer to my question:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 Gild 上花的时间不多,但我在 Project Euler 上做了很多类似类型的问题。我认为最好的方法可能是 Node.js。
如果您不熟悉,Node 基本上是在 Google V8 引擎中运行的服务器端 JavaScript。在您自己的 Mac/Windows 计算机上安装它大约需要 2 分钟。它也非常快(考虑到它是 JavaScript)。
你可以像这样使用它:
前两行之后的所有内容都可以使用你在浏览器中编写的相同 JS 完成,直接调用
console.log()
来输出回答。因此,如果您在桌面上名为
getprimes.js
的文件中编写脚本,则需要打开终端并输入node ~/Desktop/getprimes.js
(假设您使用的是 Mac)I haven't spent much time on Gild, but I do a lot of similar types of problems on Project Euler. I think the best way to go is probably Node.js.
If you're not familiar, Node is basically server-side JavaScript that runs in Google's V8 engine. Installing it on your own Mac/Windows machine takes about 2 minutes. It's also really fast (considering it's JavaScript).
And you'd use it like this:
Everything after those first two lines can be done with the same JS you'd write in the browser, right down to calling
console.log()
to spit out the answer.So, if you wrote your script in a file on your desktop called
getprimes.js
, you'd open up your terminal and enternode ~/Desktop/getprimes.js
(assuming you're on a Mac)如果您:
我强烈推荐 代码运行器。它封装了多种语言的运行时(从 C 到 JavaScript),并允许您快速构建和运行任何类型的一次性代码。只需将您的代码组合在一起,⌘R,结果就会打印在同一个窗口中。
我没有在 CodeRunner 中使用过任何基于文件的 JavaScript,但我想 kennis 的建议会适用。输出结果:
简单易行!
If you're:
I highly recommend CodeRunner. It encapsulates runtimes for a variety of languages — from C to JavaScript — and lets you quickly build and run any sort of one-off code. Just hack together your code, ⌘R, and the results are printed right there in the same window.
I haven't used any file-based JavaScript in CodeRunner, but I imagine kennis's suggestions would apply. To output your results:
Easy as pie!