Some Essential JavaScript Questions And Answers(6)

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~

猜你喜欢

转载自blog.csdn.net/fabulous1111/article/details/79800251