Javascript计算字符串

时间:2016-08-16 22:46:15

标签: javascript

我试图解决这个难题,下面是指令

  

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

     

示例:

     

longest_consec([" zone"," abigail"," theta"," form"," libe" ," zas"," theta",>" abigail"],2) - > " abigailtheta"   n是字符串数组的长度,if(n = 0)或(k> n)或(k< = 0)返回"&#34 ;;

以下是我迄今为止处理过的代码。我有解释评论他们。

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
      } 
    }
  }
}

4 个答案:

答案 0 :(得分: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));

答案 1 :(得分:0)

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

鬼鬼祟祟的部分在这里:

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;
}

答案 2 :(得分: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>

答案 3 :(得分:0)

只是为了好玩,这是一个使用array .reduce() method以及.slice().join()的版本。这基本上与其他.slice().join()答案相同,但使用.reduce()而不是明确的for循环:

&#13;
&#13;
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));
&#13;
&#13;
&#13;

或者我的代码 - 高尔夫重写以上使用箭头功能:

&#13;
&#13;
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));
&#13;
&#13;
&#13;

相关问题