Arguments Optional is this code ok

function addTogether(a,b) {

if(Number.isInteger(a)!=true) //checks if the first passed argument is a number

return undefined; //if not returns undefined snd ends the function

function inside (b) {

if (Number.isInteger(b)==true)

return b+a;

else

return undefined 

}

if(arguments.length==2 & Number.isInteger(a)==true & Number.isInteger(b)==true)

return a+b; //if the user sends 2 arguments and the arguments are numbers we return the sume

else if(arguments.length==2 & (Number.isInteger(a)!=true | Number.isInteger(b)!=true))

return undefined;

//if the user sends 2 arguments and the arguments are not numbers we return undefined

else if(arguments.length==1)

return inside; //if we pass 1 argument at a time it returns the sum

}

addTogether(2,3);

console.log(addTogether(2,"3"))

my question is because it passes all the test and it says that it is ok will this code logic be good at a real work environment or it is not thet good logic?

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

yes and no
it works, and that is the first step, and most important

but you have repeated code, and to follow a good DRY standard (Don’t Repeat Yourself)

a thing that would not be accepted as coding practice is the lack of indentetion: it is important to indent your code to favour readability, and make it easier to follow the code logic.

it will come with practice

Note tho that using isInteger will make so that it doesn’t work with decimal numbers. So you can’t do


This is the way I would write this algorithm today, I don’t think it is the best way, but it is certainly better than how I wrote it the first time I tried this challenge. Getting better comes with practice and experience, and learnign new things.

function addTogether(...args) {
  if ( args.some(el => typeof el != "number") ) {
    return;
  } else if (args.length < 2) {
    return newArg => addTogether(...args, newArg)
  } else {
    return args.reduce((x,y) => x+y)
  }
}

Why like this? For example, I return addTogether, which already includes the verification of the numbers, so I don’t have to repeat it. The first time I did this challenge I created a new function that also included the verification of numbers.