"First and foremost, thank you for mental exercise."
At first it was complicated, because of the logic, since I had to set it to false whenever I signed a new value, or even if I left parameters at fault.
At the end I got this here:
<script>
function bar(){
//foo(4); // 4
//foo(4, 4) // 8
//foo(4, function(soma){alert(soma);}) // alert(4)
foo(4, 4, function(soma){alert(soma);}) // alert(8)
}
function foo(argA, argB, callback){
var soma;
var argA = (typeof argA !== 'undefined') ? argA : 0;
var argB = (typeof argB !== 'undefined') ? argB : 0;
var callback = (typeof callback !== 'undefined') ? callback : false;
if(argA && argB && callback === false){
if(typeof argB === "function"){
callback = argB;
soma = argA;
} else {
soma = argA + argB;
}
} else if(argA && argB && callback) {
soma = argA + argB;
} else {
soma = argA;
}
if(callback && typeof callback === "function"){
return callback(soma);
} else {
return soma;
}
}
</script>
The function foo
in this case, is the function responsible for the sum, being:
foo(arg1,[arg2],[callback]) { // magia }
When creating functions of this type, you should note that javascript functions:
- Do not specify the data type for the arguments.
- Do not check what types of arguments have been passed.
- Do not check number of arguments received.
Default unsigned:
- If the function is called with fewer arguments than the declared ones, the missing elements are defined for: undefined .
The first parameter is required, or the function fails, in this example I did not create conditions in case the first parameter was not provided, so instead of returning false
if no parameter is given, the function goes return 0
as sum value.
JS is not my strong, however, I think the function is greatly reduced and the logic is correct.
Usage example:
function bar(){
//foo(4); // 4
//foo(4, 4) // 8
//foo(4, function(soma){alert(soma);}) // alert(4)
foo(4, 4, function(soma){alert(soma);}) // alert(8)
}
function foo(argA, argB, callback){
var soma;
var div = document.getElementById('demo');
var argA = (typeof argA !== 'undefined') ? argA : 0;
var argB = (typeof argB !== 'undefined') ? argB : 0;
var callback = (typeof callback !== 'undefined') ? callback : false;
if(argA && argB && callback === false){
if(typeof argB === "function"){
callback = argB;
soma = argA;
} else {
soma = argA + argB;
}
} else if(argA && argB && callback) {
soma = argA + argB;
} else {
soma = argA;
}
if(callback && typeof callback === "function"){
return callback(soma);
} else {
//return soma;
div.innerHTML = soma;
}
}
<body>
<button id="bt" onclick="bar();">Clica-me</button>
<p id="demo"></p>
</body>
Below the same function, for the same purpose, using the arguments to pass the parameters, such as the @bfavarreto example, or as @Guilherme Lautert suggested, also dynamize the order of functions:
Using the sort () method, you can create the same effect by placing the function always in the last position, making the function take its normal course.
<script>
function foo(){
var div = document.getElementById('sum');
div.innerHTML = soma(2,3,4,3); // Retorna: 8
//return soma(2,2,function(i){alert(i);}); // Retorna: alert(4)
//return soma(); // Retorna: 0
//return soma(1,function(i){alert(i);},5); // Retorna: alert(6)
//return soma(1,5, function(i){alert(i);}); // Retorna: alert(6)
}
function soma(){
var array = Array.prototype.slice.call(arguments);
var len = arguments.length;
//console.log(array.length); // Retorno: (int)N argumentos contando com o callback;
var array = array.sort(); // utilizando o método sort() a função vai estar sempre na última posicao
var callback = typeof array[len-1] == 'function' ? array.pop() : false;
var Newlen = array.length;
var soma = 0;
// Somar os valores
for(i=0; i<Newlen;++i){
soma += Number(array[i]);
}
//console.log(array.length); // Retorno: (int)N argumentos sem o callback;
//Controlar o retorno
if(callback){
// Com callback
return callback(soma);
} else {
// Sem callback
return soma;
}
}
</script>
<body>
<div id="sum"></div>
<button onclick="foo();">Somar</button>
</body>
The arguments object is similar to an array, but is not an array, the only array property that it has is the (length) size. Using the slice method on it, prevents Javascript engine optimizations. It is recommended to create an array by iterating the arguments object.