-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?

Paul
  • 26,170
  • 12
  • 85
  • 119
demboiz
  • 141
  • 1
  • 7
  • Is that an interview ? The instructions for these very basic operations look clear, what's the problem exactly ? – Denys Séguret Oct 08 '14 at 06: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 :( – demboiz Oct 08 '14 at 06:54
  • I don't understand how to modify the script so that it returns an empty string. – demboiz Oct 08 '14 at 06:55
  • Search for `javascript function default value`. Personally I would begin by reading about the `||` operator on MDN. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Logical_OR_(.7C.7C) – Felix Kling Oct 08 '14 at 06:56
  • You were banging your head on the wall for 54 minutes on 1 question, before you decided to ask for help? – Cerbrus Oct 08 '14 at 07:02
  • 2
    See http://stackoverflow.com/questions/16696642/setting-a-default-variable-value-in-a-function – Denys Séguret Oct 08 '14 at 07:02
  • There were 8 questions in the assignment. I finished the rest of the questions before re-attempting this question, then asked for help. – demboiz Oct 08 '14 at 07:04

3 Answers3

0

Default value

first = first || '';

Or, but that is not defined in the requirement

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

Apply this to both arguments

Barry
  • 3,683
  • 1
  • 18
  • 25
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;
        }
    }
Deepak Biswal
  • 4,280
  • 2
  • 20
  • 37
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.

Paul
  • 26,170
  • 12
  • 85
  • 119