2016-02-01 104 views
2

我正在創建一個基本算法來查找字符串中最長的單詞。然而,我遇到了一個問題,其中兩個數組賦予不同的變量顯示爲相等。這裏是我的代碼:Javascript變量相互影響

function LongestWord(sen) { 
 
    var arr = sen.split(' '); // arr = ['How', 'is', 'your', 'dinner'] 
 
    var wordsLength = arr.map(function(word) { // wordsLength = [3, 2, 4, 6] 
 
    return word.length; 
 
    }); 
 
    var sortLength = wordsLength.sort(function(a, b) { //sortLength = [6, 4, 3, 2] 
 
    return b - a; 
 
    }); 
 

 
    console.log(wordsLength === sortLength); 
 

 
} 
 
LongestWord("How is your dinner");

我評論我的預期每個陣列中的代碼相同。然而不知何故wordsLength正在被排序,甚至在sortLength var甚至被聲明。我知道這是因爲控制檯記錄"TRUE"。我認爲地圖可能以某種方式自動對變量進行排序,但是當我註釋掉sortLength時,wordsLength未按我原先的預期排序。 很明顯,第二個變量聲明影響第一個,但我不知道爲什麼。我不知道任何導致此行爲的概念。

+0

爲** [Array.prototype.sort()](參見https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects文檔/陣列/排序)**。 >>> sort()方法對數組中的元素進行排序並返回數組。意味着正在排序的當前數組被修改並返回。 – jherax

+0

JavaScript中的對象和數組通過引用傳遞,因此在您的代碼中wordsLength和sortLength指向相同的數組,因此它們將獲取最後修改的值。 – dexhering

+0

你究竟想要得到什麼結果,最長的單詞的長度還是最長的單詞本身? – Teemu

回答

1

array.sort()將數組排序到位返回它。 所以,你排序arrLength並將其分配給sortLength,現在它們是平等的。

1

從MDN sort是排序到位數組的元素的功能,所以你需要的數組的一個淺表副本之前排序,使用此功能slice

你的代碼已經加了新的功能,以獲得最長的一句話:

function LongestWord(sen) { 
 
    var arr = sen.split(' '); // arr = ['How', 'is', 'your', 'dinner'] 
 
    var wordsLength = arr.map(function(word) { // wordsLength = [3, 2, 4, 6] 
 
    return word.length; 
 
    }); 
 
    var sortLength = wordsLength.slice().sort(function(a, b) { //sortLength = [6, 4, 3, 2] 
 
    return b - a; 
 
    }); 
 

 
    document.body.innerHTML += '<p>' + (wordsLength === sortLength) + '</p>'; 
 

 
} 
 

 
function NewLongestWord(sen) { 
 
    if (sen.trim().length == 0) { 
 
    return -1; 
 
    } 
 
    var arr = sen.split(' ').sort(function(a, b) { //sortLength = [6, 4, 3, 2] 
 
    return b.length - a.length; 
 
    }); 
 
    return arr[0]; 
 
} 
 

 

 
document.body.innerHTML += '<p>' + (NewLongestWord("How is your dinner")) + '</p>'; 
 

 
LongestWord("How is your dinner");

+0

Sort函數實際上是對arrLength var進行排序 –

0

我建議使用sorting with map,因爲數組可以通過長度進行排序與一個臨時陣列。

更好的解決方案不是對數組進行排序。而不是排序使用迭代器,如Array#reduce,因爲只需要一次迭代,而不是更多的排序。

function longestWord(sen) { 
 
    // the array to be sorted 
 
    var list = sen.split(' '); // arr = ['How', 'is', 'your', 'dinner'] 
 
    
 
    // temporary array holds objects with position and sort-value 
 
    var mapped = list.map(function (el, i) { 
 
     return { index: i, value: el.length }; 
 
    }); 
 

 
    // sorting the mapped array containing the reduced values 
 
    mapped.sort(function (a, b) { 
 
     return b.value-a.value; 
 
    }); 
 

 
    // container for the resulting order 
 
    var result = mapped.map(function (el) { 
 
     return list[el.index]; 
 
    }); 
 
    return result.shift(); 
 
} 
 

 
function longestWord2(s) { 
 
    return s.split(' ').reduce(function (a, b) { 
 
     return a.length > b.length ? a : b; 
 
    }); 
 
} 
 

 
document.write(longestWord('How is your dinner') + '<br>'); 
 
document.write(longestWord2('How is your dinner') + '<br>');