2016-08-16 105 views
-1

我试图解决这个难题重复的条目,下方则是指令的Javascript计数字符串

现在给你一个字符串数组strarr和整数k。您的任务是返回数组中第一个由连续k个字符串组成的最长字符串。

实施例:

longest_consec([ “区”, “亚比”, “THETA”, “形式”, “libe”, “ZAS”, “THETA”> “亚比”],2) - - >“abigailtheta” n是字符串数组的长度,如果(n = 0)或(k> n)或(k < = 0)返回“”;

下面是我到目前为止工作过的代码。我有解释评论他们。

function longestConsec(strarr, k) { 

if((strarr.length == 0) || (k > strarr.length) || (k <= 0)){ 
    return ""; // solves return of empty string 
    } 

    var empty = ''; 
    var str = strarr.split(' '); // then splits into an array which can be cycled through. 

for (var i = 0; i < strarr.length; i++){ // cycle through length; 
    for(var j = 0; j < strarr[i]; j++){ // cycle through ontop of that 
    if (strarr[i] === strarr[j]){ // compare if any of cycle 1 = cycle 2 
     empty.concat(strarr[i]); // if it does, concat the word into a string 
     } 
    } 
    } 
} 
+0

???多次阅读你的问答,例子答案和答案是没有意义的。 ?字符串的顺序不重要吗?如果是这样,这意味着每个字符串的内容相同吗? – Blindman67

+0

字符串'.concat()'方法返回* new *字符串,它不会修改现有的'empty'字符串。 – nnnnnn

+0

原来的问题很糟糕。我可以阅读每一个字,但无法理解这句话。 – Leo

回答

2
function longest_consec(strarr, k) { 
    var longest = ''; 
    for (var i = 0, testString; i <= strarr.length - k; i++) { 
    testString = strarr.slice(i, i + k).join(''); 
    if (testString.length > longest.length) longest = testString; 
    } 
    return longest; 
} 
console.log(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 2)); 
0

哎呀,我有一个bug。没有尝试所有的测试用例。

偷偷摸摸的部分是在这里:

sub = arr.slice(ix, ix + k).join(''); 

你把他们给你复制一个块与slice法阵。这需要一个开始和结束值。因此,您从索引开始,并将其添加到k。然后你使用join方法将块组合在一起作为一个字符串。

现在很容易比较此字符串的长度和迄今为止找到的最长的字符串的长度。我们从空字符串开始,所以第一个块总是会更大。

如果你不知道关于slicejoin,它变得有点困难。

console.log(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 2)); 
 
console.log(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 3)); 
 
console.log(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 0)); 
 
console.log(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], -1)); 
 
console.log(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 9)); 
 

 
function longest_consec(arr, k) { 
 
    var longest = ""; 
 
    var sub = ""; 
 

 
    if(k < 0 || k > arr.length) return ""; 
 

 
    for(var ix = 0; ix < arr.length; ix++) { 
 
     sub = arr.slice(ix, ix + k).join(''); 
 
     if(sub.length > longest.length) longest = sub; 
 
    } 
 

 
    return longest; 
 
}

+0

谢谢。我添加了一小块,它工作。如果你能帮助我理解这一点,我将不胜感激。 –

+0

'function longestConsec(arr,k){0} {0} {0} var longest =“”; var sub =“”; ((arr.length == 0)||(k> arr.length)||(k <= 0)){ return'';对于(var ix = 0; ix longest.length)longest = sub; } 返回最长; }' –

0
<script> 
const Array = ["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"]; 
let bigArray = Array[0]; 
if(Array.length > 0){ 
    Array.forEach(value => { 
    if(value.length > bigArray.length){ 
     bigArray = value; 
    } 
    }); 
    console.log(bigArray); //abigail 
    //bigArray now contain the biggest string in the "Array" variable 
}else{ 
    //array is empty 
} 
</script> 
0

只是为了好玩,以下是使用.slice().join()array .reduce() method在一起的版本。这基本上是一样的其他.slice().join()答案,但使用.reduce()而不是显式的for循环:

function longest_consec(arr, k) { 
 
    if (k > arr.length || k < 1) return ''; 
 
    return arr.reduce(function(prevLongest, c, i, a) { 
 
    var str = a.slice(i, i + k).join(''); 
 
    return str.length > prevLongest.length ? str : prevLongest; 
 
    }, ''); 
 
} 
 

 
console.log(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 2)); 
 
console.log(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 9)); 
 
console.log(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 0));

还是我的代码高尔夫上面使用箭头功能重写:

var longest_consec = (a, k) => 
 
    k>a.length||k<1?'':a.reduce((p,c,i,a)=>(c=a.slice(i,i+k).join('')).length>p.length?c:p,''); 
 

 
console.log(longest_consec(["zone","abigail","theta","form","libe","zas","theta","abigail"],2)); 
 
console.log(longest_consec(["zone","abigail","theta","form","libe","zas", "theta","abigail"],9)); 
 
console.log(longest_consec(["zone","abigail","theta","form","libe", "zas","theta","abigail"],0));