javascript
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。
For example:
[舉個例子]:
Question 12:
Write a sum method which will work properly when invoked using either syntax below.
[譯]:寫一個sum方法,在使用以下任一語法調用時都能正常工作。
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)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: JavaScript数据结构与算法——列
- 下一篇: TypeScript初探
