日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Some Essential JavaScript Questions And Answers(6)

發布時間:2023/12/31 javascript 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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~

總結

以上是生活随笔為你收集整理的Some Essential JavaScript Questions And Answers(6)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。