我在JS中创建一个程序函数,它返回数组中最小的字符串。但是我总是得到一个错误返回。
这是我的代码:
function findShortestWordAmongMixedElements(arr) {
let shortest = '';
if(arr.length > 0){
for(let i = 0; i < arr.length; i++){
if(typeof arr[i] === 'string' && arr[i].length < shortest.length)
{
shortest = arr[i];
}
}
}
}
return shortest;
}
var output = findShortestWordAmongMixedElements([4, 'two', 2, 'three']);
console.log(output); // --> 'two'
我知道为什么我的代码无法正常工作?
PS。如果给定的数组不包含任何字符串,则应该返回一个空字符串。
答案 0 :(得分:2)
您有几个错误。你已经在错误的地方写了回报。你发现短串逻辑是错误的。将无穷大作为最短,然后检查较小长度的字符串。
function findShortestWordAmongMixedElements(arr) {
let shortLength = Infinity;
let shortest = "";
if (arr.length > 0) {
for (let i = 0; i < arr.length; i++) {
if (typeof arr[i] === 'string' && arr[i].length < shortLength) {
shortest = arr[i];
shortLength = arr[i].length;
}
}
}
return shortest;
}
var output = findShortestWordAmongMixedElements([4, 'two', 2, 'three']);
console.log(output); // --> 'two'
&#13;
答案 1 :(得分:2)
这是更好的逻辑实现。我们可以过滤掉字符串数组并根据字符串长度对其进行排序并返回第一个元素。
function findShortestWordAmongMixedElements(arr) {
let strings = arr.filter( x => typeof x === "string" )
.sort((a, b) => a.length - b.length);
// arr.filter gives you elements which are only strings
// arr.sort returns you elements according to their string lengths. Then you'll just have to return the first element (because it is already smallest)
return strings[0];
}
var output = findShortestWordAmongMixedElements([4, 'two', 2, 'three']);
console.log(output);
&#13;
答案 2 :(得分:1)
基本上有两个错误:
return语句在函数定义之外。
正如其他人在评论中提到的那样,您正在将变量shortest
初始化为''
,这会阻止其获取新值。
function findShortestWordAmongMixedElements(arr) {
let shortest = undefined;
if (arr.length > 0) {
for (let i = 0; i < arr.length; i++) {
if (typeof arr[i] === 'string' && (shortest == undefined ||arr[i].length < shortest.length )) {
shortest = arr[i];
}
}
}
return shortest;
}
var output = findShortestWordAmongMixedElements([4, 'two', 2, 'three']);
console.log(output); // --> 'two'
&#13;
答案 3 :(得分:1)
我相信这应该有用
function findShortestWordAmongMixedElements(arr) {
let shortest = null;
if(arr.length > 0){
for(let i = 0; i < arr.length; i++){
if(typeof arr[i] === 'string'){
if(shortest == null)
shortest = arr[i];
else if(arr[i].length < shortest.length){
shortest = arr[i];
}
}
}
}
return shortest;
}
var output = findShortestWordAmongMixedElements([4, 'two', 2, 'three']);
console.log(output); // --> 'two'
答案 4 :(得分:0)
这是你的函数,它将找到最小的字符串:
function findShortestWordAmongMixedElements(arr) {
let shortest = '';
if(arr.length > 0){
for(let i = 0; i < arr.length; i++){
if(typeof arr[i] === 'string')
{
if(shortest.length == 0) {
shortest = arr[i]; continue;
}
if(arr[i].length < shortest.length){
shortest = arr[i];
}
}
}
}
return shortest;
}
var output = findShortestWordAmongMixedElements([4, 'two', 2, 'three']);
console.log(output);
答案 5 :(得分:0)
如果过滤掉阵列然后减少它,可能会更容易:
function findShortestWordAmongMixedElements(arr) {
return arr.filter(el => typeof el === 'string')
.reduce((sht, str) => str.length < sht.length ? str : sht);
}
var output = findShortestWordAmongMixedElements([4, 'two', 2, 'three']);
console.log(output); // --> 'two'
&#13;
答案 6 :(得分:-1)
function tinyFriend(arr){
var tiny = arr[0];
for (let i = 0; i < arr.length; i++) {
const element = arr[i];
if( tiny.length > element.length){
tiny = element
}
}
return tiny
}
var friend = ["abir","abdullah","robin","abdurrohim","ali"]
var smallFrind = tinyFriend(friend)
console.log(smallFrind)