Arguments Optional

From the FreeCodeCamp intermediate algorithms here:

Create a function that sums two arguments together. If only one argument is provided, then return a function that expects one argument and returns the sum.

For example, addTogether(2, 3) should return 5, and addTogether(2) should return a function.

Calling this returned function with a single argument will then return the sum:

var sumTwoAnd = addTogether(2);

sumTwoAnd(3) returns 5.

If either argument isn’t a valid number, return undefined.

FreeCodeCamp – Arguments Optional

Forgive me for not writing out my thought process! I got excited for getting so close to the end of the Intermediate algorithms and just moved on! My code is below though!

My solution:

function addTogether() {
  let x = arguments[0];
  let y = arguments[1];

  if (typeof x !== 'number') {
    return undefined;
  }
  
  if (typeof x === 'number' && typeof y === 'number') {
    // console.log('yess')
    return x + y;
  } else if (typeof y === 'undefined') {
    return function() {
      if (typeof arguments[0] === 'number') {
        return x + arguments[0];
      } else {
        return undefined;
      }
    }
  }
}

addTogether(2, 3);

Published by Zachary

Learning to code with free code camp - zed.code.blog. Reading and writing at alwaysbooks.co.uk.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website at WordPress.com
Get started
%d bloggers like this: