Need help with intermediate algorithm

Here is the original problem: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/arguments-optional

My code :

function addTogether(x, y) {

if (typeof y === “undefined”) {

return function (y) {

return x + y;

}

}

return x + y;

}

This solves all, but the problems that should return undefined.

I can solve the problems that should return undefined with:

if (!Number.isInteger(y))

return undefined;

return x + y;

}

I don’t know how to put this all together since only one part seems to work at a time. Is my code wrong altogether? Thanks for taking a look.

You have all of the right pieces. I would start by making a flowchart of sorts:

  1. If {this is true} then {return undefined}

  2. If {this is true} then {return the sum of x and y}

  3. If {this is true} then {return a function}

How would you fill out this chart I’ve started? What goes in the three {this is true} spots?

Is this correct?

  1. If (!Number.isInteger(y))} then {return undefined}

  2. If {Number.isInteger(y))} then {return the sum of x and y}

  3. If {(typeof y === “undefined”)} then {return a function}

You’re close. We have a logical issue and a syntax issue here:

First the logical issue. The challenges says that

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

So the logic for 1. needs to be a bit different.
We might need to make some of the other if {} more complicated, but let’s get there when we get there : )

For the syntax issue, isInteger won’t quite check for numbers, only integers.

Thanks for the suggestions. I’ll work on this.

Feel free to post questions as you run into them!

This seems to work…am I on the right track?

if (typeof y !== “number” || typeof x !== “number”) {

return undefined

} else {

return x + y;

}

You are definitely on the right track! One question to ponder though, what is typeof undefined? If you check for if y is not a number in 1., then will you ever be able to reach 3.?

Not sure what is wrong…

function addTogether(x, y) {

if (typeof y === ‘undefined’) {
return function (y) {
return x + y;
}
}

else if (typeof x !== ‘number’ || typeof y === ‘number’) {

return x + y;
} else {

undefined;
}}

You have some smart quotes in there, which is causing issues for my code

function addTogether(x, y) {
  if (typeof y === 'undefined') {
    return function (y) {
      return x + y;
    }
  } else if (typeof x !== 'number' || typeof y === 'number') {
    return x + y;
  } else {
    undefined;
  }
}

I adjusted the code so that it’s a bit easier for me to read and I removed the smart quotes.

There are a few little things now

  1. In your first if, you still want to make sure that x is a number.
  2. The function you return should check if ‘y’ is a number.
  3. Is your condition quite what you want for the else if? You need both x and y to be a number in this case, right?

I fixed the things you listed correctly, right?



function addTogether(x, y) {
  if (typeof x === 'number' && typeof y === 'undefined') {
    return function (y) {
        if(typeof y === 'number')
      return x + y;
    }
  } else if (typeof x === 'number' || typeof y === 'number') {
    return x + y;
  } else {
    undefined;
  }
}

You are really close now!

  } else if (typeof x === 'number' || typeof y === 'number') {

This say, is words, “otherwise, if x is a number or y is a number, then …”

Is that what you meant the logic to say?

1 Like

Nope, I meant the logic to say if both are numbers.

function addTogether(x, y) {

  if (typeof x === 'number' && typeof y === 'undefined') {

    return function (y) {

        if(typeof y === 'number')

      return x + y;

    }

  } else if (typeof x === 'number' && typeof y === 'number') {

    return x + y;

  } else {

    undefined;

  }

}
1 Like

Hey, that code passes for me!

1 Like

Thanks so much for the time you spent helping me out. I really appreciate you helping me understand what I’m actually doing and not just giving me the answer.

2 Likes

You did all of the hard work! I just helped you put together the pieces you had. Congrats

2 Likes

just to nitpick, this doesn’t do anything. The only reason for which your function returns undefined correctly it is because that is the default returned value, this snippet above doesn’t return

1 Like