Some Essential JavaScript Questions And Answers

Question11:

Write a simple function (less than 160 characters) that returns a boolean indicating whether or not a string is a palindrome.

[译]:写一个简单的方法(少于169个字符),要求返回布尔值指明字符串是否是回文结构。

Palindrome: a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction. Famous examples include “Amore, Roma“, “A man, a plan, a canal: Panama” and “No ‘x’ in ‘Nixon’“.

[译]:回文:一个单词,短语,数字,或者其他符号或者元素的顺序,其意义可以从正方向和反方向解释(则成为回文结构)。一些出名的例子包括“Amore, Roma”,“A man,a plan,a canal:Panama”以及“No 'x' in 'Nixon'”。

Anwer:

The following one line function will return true if str is a palindrome; otherwise, it returns false.

[译]:下面这个方法在字符串符合回文结构时返回true,否则返回fasle。

function isPalindrome(str) {str = str.replace(/\W/g, '').toLowerCase();return (str == str.split('').reverse().join(''));
}

For example:

[举个例子]:

console.log(isPalindrome("level"));                   // logs 'true'
console.log(isPalindrome("levels"));                  // logs 'false'
console.log(isPalindrome("A car, a man, a maraca"));  // logs 'true'

解释:代码中使用了正则表达式,\W 元字符用于查找非单词字符。单词字符包括:a-z、A-Z、0-9,以及下划线。

Question 12:

Write a sum method which will work properly when invoked using either syntax below.

[译]:写一个sum方法,在使用以下任一语法调用时都能正常工作。

console.log(sum(2,3));   // Outputs 5
console.log(sum(2)(3));  // Outputs 5

Answer:

There are (at least) two ways to do this:

[译]:至少有两种方法去实现

METHOD 1

function sum(x) {if (arguments.length == 2) {return arguments[0] + arguments[1];} else {return function(y) { return x + y; };}
}

In JavaScript, functions provide access to an arguments object which provides access to the actual arguments passed to a function. This enables us to use the length property to determine at runtime the number of arguments passed to the function.

[译]:在JavaScript中,函数可以提供对 arguments对象的访问,而arguments对象提供传递到函数的实际参数的访问。这使我们能够使用length属性来确定在运行时传递给函数的参数的个数。

If two arguments are passed, we simply add them together and return.

[译]:如果传入两个参数,我们只需要将他们简单地相加并返回。

Otherwise, we assume it was called in the form sum(2)(3), so we return an anonymous function that adds together the argument passed to sum() (in this case 2) and the argument passed to the anonymous function (in this case 3).

[译]:否则,我们认为它是以sum(2)(3)的形式调用,所以我们返回一个用于求传入sum()的参数(在此例中为2)以及传入匿名函数的参数(在此例中为3)之和的匿名函数。

METHOD 2

function sum(x, y) {if (y !== undefined) {return x + y;} else {return function(y) { return x + y; };}
}

When a function is invoked, JavaScript does not require the number of arguments to match the number of arguments in the function definition. If the number of arguments passed exceeds the number of arguments in the function definition, the excess arguments will simply be ignored. On the other hand, if the number of arguments passed is less than the number of arguments in the function definition, the missing arguments will have a value of undefined when referenced within the function. So, in the above example, by simply checking if the 2nd argument is undefined, we can determine which way the function was invoked and proceed accordingly.

[译]:当调用一个函数的时候,JavaScript不要求参数的数目匹配函数定义中的参数数量。如果传递的参数数量超过函数中定义的参数数量,那么多余参数将简单地被忽略。此外,如果传递的参数数量少于函数定义中的参数数量,那么缺少的参数在函数中被引用时将会给一个 undefined值。所以,在上面的例子中,简单地检查第2个参数是否是undefined,我们就可以相应地确定函数被调用以及行进的方式。

翻译此文章目的在于提升英语水平以及回顾JS基础,有错误欢迎指出,O(∩_∩)O~

Some Essential JavaScript Questions And Answers(6)相关推荐

  1. Some Essential JavaScript Questions And Answers(5)

    Some Essential JavaScript Questions And Answers Question 9: Discuss possible ways to write a functio ...

  2. Some Essential JavaScript Questions And Answers(4)

    Some Essential JavaScript Questions And Answers Question7: What is NaN? What is its type? How can yo ...

  3. Some Essential JavaScript Questions And Answers(3)

    Some Essential JavaScript Questions And Answers Question5: What is the significance, and what are th ...

  4. Some Essential JavaScript Questions And Answers(2)

    Some Essential JavaScript Questions And Answers Question3: What will the code below output to the co ...

  5. Some Essential JavaScript Questions And Answers(1)

    一些很经典的JavaScript探讨题,分享分享,英语好的可以忽略我的翻译. Some Essential JavaScript  Questions [译]:一些必要的(基本的)JS面试题及答案 Q ...

  6. mega_[MEGA DEAL] 2018 Essential JavaScript编码捆绑包(96%折扣)

    mega 学习世界上最重要的Web开发语言的终极指南(超过29小时,超过900页!) 嘿,怪胎, 本周,在我们的JCG Deals商店中,我们提供了另一个超值优惠. 我们将在2018 Essentia ...

  7. 35+ Top Apache Tomcat Interview Questions And Answers【转】

    原文地址:https://www.softwaretestinghelp.com/apache-tomcat-interview-questions/ Most frequently asked Ap ...

  8. [笔记一]Essential JavaScript Design Patterns For Beginners

    最近在看Essential JavaScript Design Patterns For Beginners 原文地址:http://www.addyosmani.com/resources/esse ...

  9. C# Interview Questions and Answers

    What's C# ?C# (pronounced C-sharp) is a new object oriented language from Microsoft and is derived f ...

最新文章

  1. Bert时代的创新:Bert应用模式比较及其它 | 技术头条
  2. give root password for maintenance 启动异常的解决
  3. python的快速入门-Python快速入门,你想要的就在这里了!
  4. java操作大文件复制
  5. MongoEngine MongoDB 的 ORM 库
  6. php获取flash上传视频文件大小,php解析flash文件(.swf文件)获取其长度和宽度
  7. mysql 行列转换 动态_mysql 行列动态转换的实现(列联表,交叉表)
  8. linux mysql cpu 查看工具_Linux监控工具-Nmon命令行:Linux系统性能的监测利器
  9. VS2010 打包生成exe文件后 执行安装文件出现 TODO:lt;文件说明gt;已停止工作并已关闭...
  10. java.lang.NoClassDefFoundError:org/apache/commons/logging/LogFactory
  11. [jquery视频教程 初级+高级][25课程]
  12. 基于javaweb宠物领养平台管理系统设计和实现
  13. U磁盘检测和大量收集量产工具
  14. 白帽子讲Web安全(纪念版)笔记
  15. 工厂模式概念及其使用场景
  16. 将logstash与elasticsearch性能匹配的几点尝试
  17. 混合波束成形|进阶:深入浅出混合波束赋形
  18. 下载Nexus老版本war包
  19. org.apache.hadoop.hdfs.server.namenode.LeaseExpiredException: No leas e on
  20. 美国食品药品监督管理局宣布将研究区块链技术

热门文章

  1. [UOJ62]怎样跑得更快
  2. Homebrew替换源
  3. 《A Novel Pipeline Approach for Efficient Big Data Broadcasting》阅读报告
  4. 安防监控应用成LED企业新盈利点
  5. Pascal's Triangle 2(leetcode java)
  6. 【推荐】国外优秀Drupal答疑网站
  7. Cisco三层交换机DHCP中继简单配置
  8. linux系统安装jdk及配置环境变量
  9. idea:忽略大小写提示设置
  10. chrom浏览器-F2使用方法一