// 先比较数组中字符串的长度,取最短长度的字符串作为for循环的终止条件
function getLen(Array) {
let Len = Array[0].length
for(let i=1;i<Array.length;i++){
if(Array[i].length<Len){
Len = Array[i].length
}
}
return Len
}
var getEndString = function(Array) {
let Len = getLen(Array)
let EndString =''
for(let i=0;i<=Len;i++){
for(let j=1;j<Array.length;j++){
if(Array[0].charAt(Array[0].length-i-1) != Array[j].charAt(Array[j].length-i-1) && i!=0){
console.log('数组中字符串的相同结尾是'+EndString)
return false
} else if(Array[0].charAt(Array[0].length-i-1) != Array[j].charAt(Array[j].length-i-1) && i == 0){
console.log('没有相同的结尾')
return false
}
}
EndString = Array[0].charAt(Array[0].length-i-1) + EndString
}
}
let Array = ['test','wyrjjst','sjdkjdst','tybhbst']
getEndString(Array)
网友评论