JavaScript 面試題:重復輸出一個給定的字符串
其實這是可以作為一道很好的面試題,可以考察開發人員的綜合能力。
面試題:重復輸出一個給定的字符串( str 第一個參數)n 次 ( num 第二個參數),如果第二個參數 num 不是正數的時候,返回空字符串。
function repeatStringNumTimes(str, num) { return str;}repeatStringNumTimes('abc', 3);
提供測試情況:
repeatStringNumTimes('*', 3) //應該返回 '***'.repeatStringNumTimes('abc', 3) //應該返回 'abcabcabc'.repeatStringNumTimes('abc', 4) //應該返回 'abcabcabcabc'.repeatStringNumTimes('abc', 1) //應該返回 'abc'.repeatStringNumTimes('*', 8) //應該返回 '********'.repeatStringNumTimes('abc', -2) //應該返回 ''. 解題思路:
我將介紹三種方法:
使用 `while` 循環 使用遞歸 使用ES6 `repeat()`方法1:通過 `while` 循環重復輸出一個字符串
這可能是最常規的解題思路。 while 語句只要指定的條件計算結果為 true 的時候,就執行其語句。 while 語句結構大概是這樣的:
while (condition) statement
在每次通過循環之前計算條件結果。如果條件為 true ,則執行語句。如果條件為 false ,則執行繼續 while 循環之后的任何語句。
只要條件為 true ,語句就會執行。 這里是解決方案:
function repeatStringNumTimes(string, times) { // 第1步. 常見一個空字符,用來寄存重復的字符串 var repeatedString = ''; // 第2步. 設置 while 循環的條件為(times > 0) 作為檢查 while (times > 0) { // 只要 times 大于 0, 語句就會執行 // 執行語句 statement repeatedString += string; // 等價于 repeatedString = repeatedString + string; times--; // 遞減,等價于 times = times - 1; } /* while循環邏輯 條件T/F repeatedString += string 結果 次數 1th (3 > 0) true '' + 'abc''abc' 2 2th (2 > 0) true 'abc' + 'abc' 'abcabc' 1 3th (1 > 0) true 'abcabc' + 'abc' 'abcabcabc' 0 4th (0 > 0) false } */ // 第3步. 返回重復字符串 return repeatedString; // 'abcabcabc'}repeatStringNumTimes('abc', 3);
去掉注釋后:
function repeatStringNumTimes(string, times) { var repeatedString = ''; while (times > 0) { repeatedString += string; times--; } return repeatedString;}repeatStringNumTimes('abc', 3);
好,輕松完成!不過這里還可以有幾個變種:
對于老前端來說,首先一個可能會將字符串拼接,修改為 數組 join() 拼接字符串,例如:
function repeatStringNumTimes(string, times) { var repeatedArr = []; // while (times > 0) { repeatedArr.push(string); times--; } return repeatedArr.join('');}repeatStringNumTimes('abc', 3)
很多老前端都有用數組 join() 拼接字符串的“情懷”,因為很早以前普遍認為數組 join() 拼接字符串比字符串 + 拼接速度要快得多。不過現在未必,例如,V8 下 + 拼接字符串,要比數組 join() 拼接字符串快。我用這兩個方法測試了3萬次重復輸出,只相差了幾毫秒。
另一個變種可以用 for 循環:
function repeatStringNumTimes(string, times) { var repeatedString = ''; for(var i = 0; i < times ;i++) { repeatedString += string; } return repeatedString;}repeatStringNumTimes('abc', 3)
方法2:通過條件判斷和遞歸重復輸出一個字符串
遞歸是一種通過重復地調用函數本身,直到它達到達結果為止的迭代操作的技術。為了使其正常工作,必須包括遞歸的一些關鍵特征。
第一種是基本情況:一個語句,通常在一個條件語句(如 if )中,停止遞歸。
第二種是遞歸情況:調用遞歸函數本身的語句。
這里是解決方案:
function repeatStringNumTimes(string, times) { // 步驟1.檢查 times 是否為負數,如果為 true 則返回一個空字符串 if (times < 0) { return ''; } // 步驟2.檢查times是否等于1,如果是,返回字符串本身。 if (times === 1) { return string; } // 步驟3. 使用遞歸 else { return string + repeatStringNumTimes(string, times - 1); // return 'abcabcabc'; } /* 遞歸方法的第一部分你需要記住,你不會只調用一次,您將有好幾個嵌套調用 times string + repeatStringNumTimes(string, times - 1) 1st call 3 'abc' + ('abc', 3 - 1) 2nd call 2 'abc' + ('abc', 2 - 1) 3rd call 1 'abc' => if (times === 1) return string; 4th call 0 '' => if (times <= 0) return ''; 遞歸方法的第二部分 4th call will return '' 3rd call will return 'abc' 2nd call will return 'abc' 1st call will return 'abc' 最后調用是串聯所有字符串 return 'abc' + 'abc' + 'abc'; // return 'abcabcabc'; */}repeatStringNumTimes('abc', 3);
去掉注釋后:
function repeatStringNumTimes(string, times) { if(times < 0) return ''; if(times === 1) return string; else return string + repeatStringNumTimes(string, times - 1);}repeatStringNumTimes('abc', 3);
方法3:使用ES6 `repeat()` 方法重復輸出一個字符串
這個解決方案比較新潮,您將使用 String.prototype.repeat() 方法:
repeat() 方法構造并返回一個新字符串,該字符串包含被連接在一起的指定數量的字符串的副本。 這個方法有一個參數 count 表示重復次數,介于0和正無窮大之間的整數 : [0, +∞) 。表示在新構造的字符串中重復了多少遍原字符串。重復次數不能為負數。重復次數必須小于 infinity,且長度不會大于最長的字符串。
這里是解決方案:
function repeatStringNumTimes(string, times) { //步驟1.如果 times 為正數,返回重復的字符串 if (times > 0) { // (3 > 0) => true return string.repeat(times); // return 'abc'.repeat(3); => return 'abcabcabc'; } //Step 2. Else 如果times是負數,如果為true則返回一個空字符串 else { return ''; }}repeatStringNumTimes('abc', 3);
去掉注釋后:
function repeatStringNumTimes(string, times) { if (times > 0) return string.repeat(times); else return '';}repeatStringNumTimes('abc', 3);
您可以使用三元表達式作為 if/else 語句的快捷方式,如下所示:
function repeatStringNumTimes(string, times) { times > 0 ? string.repeat(times) : '';}repeatStringNumTimes('abc', 3);
面試官可能會根據欣賞這樣的簡潔代碼。
來自:http://www.css88.com/archives/7045
相關文章: