0

When I run this:

var array= [123, ["hello", 67, 888888], "try", {key: "key", value: "this is the longest String"}]
longestString(array);

correct answer:
>>>"this is the longest String" 

current answer:
>>>"try" 

How can I update this function to pass the above test and return the correct answer?

below is the longestString() function:

function longestString(array) {
    // return the longest string in the array
    var longest = 0;
    var longestString = "";
    for (var i = 0; i < array.length; i++) {
        if (array[i].length > longest && typeof array[i] =="string") {
        longest = array[i].length;
        longestString = array[i];
        }
    }
    return longestString;
}
3
  • because the nested array length is longer (5) than the string 'tiny' (4) Commented Aug 23, 2015 at 2:02
  • Your requirements actually aren’t clear. Why should the array be shorter than "tiny"? Do you want to compare every Array element, no matter how nested it is? What about objects with key-value pairs or ES6 Symbols? How should these cases be handled? Commented Aug 23, 2015 at 5:08
  • well the array isn't a string it's an array... it asks for the longest string. However, I would like to update this to also check all strings within nested arrays and or nested objects as well. Would you have a solution to check for these conditions as well? When I run this: longestString([123, ["hello", 67, 888888], "try", {key: "key", value: "this is the longest String"}]) The answer should be "this is the longest String" but it says "try" is the longest string.. Commented Aug 23, 2015 at 5:18

1 Answer 1

6

Add a typeof to check that data is string or not

like this

function longestString(array) {
    // return the longest string in the array
    var longest = 0;
    var longestString = "";
    for (var i = 0; i < array.length; i++) {
        if (array[i].length > longest && typeof array[i] ==="string") {
        longest = array[i].length;
        longestString = array[i];
        }
    }
    return longestString;
}

EDIT

for strings within nested arrays/ objects?

Try like this

function longestString(array) {
    // return the longest string in the array
    var longest = 0;
    var longestString = "";
    for (var i = 0; i < array.length; i++) {
        if (Object.prototype.toString.call(array[i]) === '[object Array]') {
            array[i].forEach(function (it) {
                if (it.length > longest) {
                    longest = it.length;
                    longestString = it;
                }
            })
        } else if (Object.prototype.toString.call(array[i]) === '[object Object]') {
            for (var j in array[i]) {
                if (array[i][j].length > longest) {
                    longest = array[i][j].length;
                    longestString = array[i][j];
                }
            }
        } else if (typeof array[i] == "string" && array[i].length > longest) {
            longest = array[i].length;
            longestString = array[i];
        }
    }
    return longestString;
}

console.log(longestString([123, ["hello", 67, 888888], "try", {
    key: "key",
    value: "this is the longest String"
}]));
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Anik! This worked after I changed "===" to "=="
=== is strict checking it don't convert during checking and == this use conversion during checking. as typeof always return string === or == shouldn't be the problem. By the way you are welcome :)
Would you know how to update the function so that it would consider to look at strings within nested arrays/ objects? When I run this: longestString([123, ["hello", 67, 888888], "try", {key: "key", value: "this is the longest String"}]) The answer should be "this is the longest String" but it says "try" is the longest string..
Awesome ANIK! You are the best :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.