Incorrect Function? Why?

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

function addTogether(...a) {
if(a.length > 1){
  if(typeof(a[0]) == "number" && typeof(a[1]) == "number"){
    return a[0] + a[1];
  }
}else{
  return function(b){
    if(typeof(a) == "object" && typeof(b) == "number"){
      return parseInt(a) + parseInt(b);
    }
  }
}

if(typeof(a[0]) == "string"){
  return undefined;
}


} 



console.log(addTogether("https://www.youtube.com/watch?v=dQw4w9WgXcQ"));




  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36

Challenge: Arguments Optional

Link to the challenge:

Here you should use a curried function for this purpose.

Check out this fCC link:
Functional Programming: Introduction to Currying and Partial Application | freeCodeCamp.org

I think he is using a curried function and in the proper place.

I think the issue is the order of your logic. I think you need to rethink where you checking if the first argument in the string. I was able to reorder your logic and get this function to pass.

1 Like

I use curried function in my else

I try step by step with simple console.log(“I’m here”); but he never star in my firts if, and I don’t understand why if don’t go in the first if don’t go to my second if

1 Like

when would you expect for it to execute?

Oh ok i just saw that.

The first if don’t get and the second don’t get. The execute is in the second if I think my code like a secuencial process

if the first if doesn’t execute, the else execute and there is a return statement inside the else

If your code is called with a single string, will this ever run?

if(typeof(a[0]) == "string"){
  return undefined;
}

Trace through the code.


Yes my code run correct whit this if.

Personally, I think your logic is backwards. You are checking if both arguments are valid numbers first and then trying to work your way through all the exceptions. You might consider approaching this from the other end and start by looking for when they are not numbers first and ruling those out which will then leave you with two valid numbers to add together at the end.

Also, I wouldn’t use the rest operator here, it’s only two arguments, so I think it would be much simpler to just use (a, b) that way you don’t have to mess with array notation.

Finally, I’m not sure this challenge requires you to convert strings to numbers, so I would leave that out to make this simpler. In other words, you only need to be testing if the arg is a “number”. Remember, there are other types besides “string” and “number”, such as “boolean” and “object”. Don’t you think it would be much easier to just test if an argument is not a “number” rather than testing if it is one of the other non-number types?

Maybe the solution is try check first different, I can’t use (a, b) because in the exercise need curried function. With my if…else never get and the logic say if don’t get in the first if go to else but if don’t get in my else he go to my second if. But don’t get.

This doesn’t make sense to me. Please elaborate. I can tell you that I solved this challenge using (a, b) (or if you don’t like those names use something else, like (firstNum, secondNum). Also, you are using a in your returned function. What is the value of a?

Your parameters (…a) are correct. But you need to check first if the arguments are numbers and not strings or arrays. You need to do this once for the outer function and then again for the inner one.
Since isNaN() doesn’t work in all cases, you can use typeof which is also correct! But you need to also check if the argument passed is an array or not. There is a built-in function for that.

Once you do that check, return undefined right away if the argument passes that test. Or else (if the argument is not a string neither an array), then get to the part of checking if the a.length > 1. And then again do the similar type check in your inner function also.

If you follow these steps, it works.

Yes my code run correct whit this if.

No, my point was to trace through the original code with that condition. Will it ever reach that last if?

As I’m saying, your original code is easily fixable. With logic, sometimes the order matters. Take this pseudo code example

function wakeUpInTheMorning
  if well rested
    take shower
    eat breakfast
    exit function
  else
    go back to sleep
    set 10 minute delay
    call function wakeUpInTheMorning
    exit function
  
  if house on fire
    call 911
    exit function

I’m suggesting that the logic is out of order. I’m suggesting that if the house is on fire, then none of that other stuff matters and the rest of the stuff will just distract from the more important issue. It’s what we call a “fail-fast” - a condition that means it’s impossible to proceed so we just fail for it right away. If the first (or any) argument is a string, there is no point in continuing. Like the 911 call, it should go at the top. It is a breaking condition that supersedes all others - there is no point in pretending to continue.

Yes, there are other ways you could simplify your logic, but moving the logic around will work. You have all the ideas there, they are just in the wrong order.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.