Reputation:
I am creating a program function in JS that returns the smallest string in array. However I always get an error return.
Here's my code:
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'
Any idea what am I doing wrong why my code won't work?
PS. If the given array contains no strings, it should return an empty string.
Upvotes: 0
Views: 4034
Reputation: 1
function tinyFriend(arr) {
var tiny = arr[0];
for (let i = 0; i < arr.length; i++) {
var element = arr[i];
if (tiny.length > element.length) {
tiny = element;
}
}
return tiny;
}
var friend = ["Rohim", "Korim", "Bokkar", "Jobbar", "Noor"]
var smallFrind = tinyFriend(friend)
console.log("No.4: Your tiny friend name is", smallFrind)
for (let i = 0; i < arr.length; i++) {
var element = arr[i];
if (tiny.length > element.length) {
tiny = element;
}
}
return tiny;
}
Upvotes: 0
Reputation: 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)
Upvotes: -1
Reputation: 14423
It might just be easier if you filter out the array and then reduce it:
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'
Upvotes: 0
Reputation: 16547
Here's a better implementation of your logic. We can just filter out string arrays and sort it out according to string length and return first element.
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);
Upvotes: 1
Reputation: 581
Here is your function which will find the smallest string:
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);
Upvotes: 0
Reputation: 10458
I believe this should work
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'
Upvotes: 0
Reputation: 14541
Essentially there are two bugs:
The return statement is outside the function definition.
As others have mentioned in the comments, you are initializing the variable shortest
to ''
which prevents it from taking new value.
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'
Upvotes: 0
Reputation: 121998
You have several errors. You have written your return in wrong place. And your finding short string logic is wrong. Take infinity as the shortest and then check against smaller length string.
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'
Upvotes: 1