-7

These are the instructions to the script I have to write:

 function longest(first, second) {
    if (first.length >=  second.length) {
        return first;
    } else {
        return second;
    }

Use the || operator to specify default values for first and second in the function. If one or both parameters are not specified, the empty string should be used as the default value.

Once you make your changes, you should be able to test the function as follows:

console.log(longest('Alice'));   // second is undefined - second defaults to the empty string
//Alice

console.log(longest());    // both first and second are undefined - both default to the empty string
//(an empty string)

console.log(longest('hi','hello'));
//hello

console.log(longest('hi', 'me'));
//hi

console.log(longest(''));
//(an empty string)

I don't even know where to begin. Can someone shed some light for me?

7
  • Is that an interview ? The instructions for these very basic operations look clear, what's the problem exactly ? Commented Oct 8, 2014 at 6:53
  • no, it's just for the class I'm taking. 6 minutes till it's due and I've been stuck for an hour lol :( Commented Oct 8, 2014 at 6:54
  • I don't understand how to modify the script so that it returns an empty string. Commented Oct 8, 2014 at 6:55
  • Search for javascript function default value. Personally I would begin by reading about the || operator on MDN. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Oct 8, 2014 at 6:56
  • 2
    See stackoverflow.com/questions/16696642/… Commented Oct 8, 2014 at 7:02

3 Answers 3

0

Try this:

function longest(first, second) {
        var firstDefault = '';
        var secondDefault = '';
        first = typeof first !== 'undefined' ? first : firstDefault;
        second = typeof second !== 'undefined' ? second : secondDefault;
        if (first.length >=  second.length) {
            return first;
        } else {
            return second;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Default value

first = first || '';

Or, but that is not defined in the requirement

first = (typeof first !== 'undefined') ? first : '';

Apply this to both arguments

Comments

0

The issue with

 function longest(first, second) {
    if (first.length >=  second.length) {
        return first;
    } else {
        return second;
    }
  }

if you call it as longest('Alice') is that it spews out an error:

TypeError: Cannot read property 'length' of undefined

because second is undefined, and properties of undefined like .length can not be read.

undefined is actually a thing in Javascript rather than an automatic error like in many other languages. We'll come back to that soon...

The purpose is to get you to thinking about how to fix the function. If the function assigned a blank string in place of undefined, the blank string would have a length, 0, that could be read.

In Javascript the || operator can be used to assign default values to missing variables as follows:

 function longest(first, second) {
    var defaultValue = ''; 
    var first = first || defaultValue; 
    var second = second || defaultValue;
    if (first.length >=  second.length) {
        return first;
    } else {
        return second;
    }
  }

Now if either first or second is undefined, they will be replaced locally with the value of the defaultValue variable, which here is set to the empty string ''.

The empty string is not an undefined value, it is a string that contains no characters. It has a length, and that length is zero. Now the if statement will not fail when an undefined value is passed.

Now longest('Alice') yields 'Alice'

Bugs

Unfortunately the assignment as shown does not teach you enough about Javascript. It is probably worth knowing about a peculiar Javascript feature: any property whether it is called 'length' or something else can be read from existing objects that do not have that property. This may lead to undesired behavior. The result is a value called undefined, which is a value that things can be in Javascript.

When undefined is compared with a number, the result is always false. Mathematically that's normally impossible. We normally think that if x>1 is false, then x<1 must be true or x is 1. That kind of logic does not work for undefined.

When undefined is compared with undefined the result is also false, unless it is an equality test which is true.

Why does this matter? It relates to bugs in the longest() function above. Number inputs are one example. Strings representing numbers have a length but numbers do not have a length. Reading the length of a number yields undefined. And comparing undefined with a defined number is false. That means we can do this:

longest(1,100) returns 100 Correct.

but

longest(100,1) returns 1 OOPS.

Comments

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.