FreeCodeCamp - Arguments Optional - Need Help

FreeCodeCamp - Arguments Optional - Need Help
0

#1

Hello, to make this exercice work, it is important to store the value from my …args in a variable ?
I can’t use arrayArgs[0] and arrayArgs[1] ? i tried and it didn’t work.
So i did my firstValue and secondValue variable…
I just need clarification on this:

/*
Link: https://beta.freecodecamp.org/en/challenges/intermediate-algorithm-scripting/arguments-optional
*/

function addTogether( ...args ) {
	const arrayArgs = [...args];
	const firstValue = arrayArgs[0];
	const secondValue = arrayArgs[1];
	const arrayArgsL = arrayArgs.length;
	
	if( ( arrayArgsL == 2 ) && ( Number.isInteger( firstValue ) == true ) && ( Number.isInteger( secondValue ) == true  ) ) {
		return firstValue + secondValue;
		
	}else if( ( Number.isInteger( firstValue ) == false ) && ( Number.isInteger( secondValue ) == false ) ) {
		return undefined;
	
	}else if( arrayArgsL == 1 ) {
		return function( b ) {
			if( ( Number.isInteger( firstValue ) == true ) && ( Number.isInteger( b ) == true ) )  {
				return firstValue + b;
			}else {
				return undefined;
			}
		};
	}
};

console.log( addTogether( 2, 3 ) );
console.log( addTogether(2)(3) );
console.log( addTogether("http://bit.ly/IqT6zt") );
console.log( addTogether(2, "3") );
console.log( addTogether(2)([3]) );

Thx you !!

ps: If you have any idea concerning this exercice too:


#2

Can you post what you tried to do with arrayArgs[0] and arrayArgs[1] that did not work? I think I know what you might have done wrong, but want to make sure before I comment.


#3

Hello, well every time you see a firstValue && secondValue, i used arraysArgs[0] or arraysArgs[1] :slight_smile:

But, how function like this one function()() is called exactly ? I know about rest argument but not this and i didn’t saw it, if i remember correctly, anywhere in all my courses (FCC and elsewhere).


#4

When I replaced firstValue with arraysArgs[0] and replaced secondValue with arraysArgs[1] (as seen below), your solution passes the FCC tests:

function addTogether( ...args ) {
	const arrayArgs = [...args];
	const arrayArgsL = arrayArgs.length;
	
	if( ( arrayArgsL == 2 ) && ( Number.isInteger( arrayArgs[0] ) == true ) && ( Number.isInteger( arrayArgs[1] ) == true  ) ) {
		return arrayArgs[0] + arrayArgs[1];
		
	}else if( ( Number.isInteger( arrayArgs[0] ) == false ) && ( Number.isInteger( arrayArgs[1] ) == false ) ) {
		return undefined;
	
	}else if( arrayArgsL == 1 ) {
		return function( b ) {
			if( ( Number.isInteger( arrayArgs[0] ) == true ) && ( Number.isInteger( b ) == true ) )  {
				return arrayArgs[0] + b;
			}else {
				return undefined;
			}
		};
	}
}

#5

You could have the following:

function chainFunction(...args) {
  return function(v1) {
    var argValue = v1 + args[0];
    return function(v2) {
      return v2 + argValue;
    }
  }
}
chainFunction(1)(5)(10); // yields 16

Since you call each function with only one argument, args[0] will be the 1, v1 will be the 5, and v2 will be the 10. You are returning a function that executes the next (?) in sequence.


#6

Ok, thanks you !
But this method is not really used ? is it ?