How to discard an empty array in a function?

1

Hello everyone I am working with native js, in this function I return the product of the elements in the array, what I can not achieve is to discard the case of an empty array, it always shows me the 1, but if it resulted in change the value to 0 does not multiply me. I appreciate your help to all, greetings.

function getProductOfAllElementsAtProperty(obj, key) {
  var arr = obj[key];
 if(arr === [] || !Array.isArray(arr)){
    return 0;
 }
  else{
    var result = 1;
    for(var i = 0; i<arr.length; i++){
      result = result *= arr[i];
      console.log(result);
    }
     return result;
  }
}

var obj = {
  key:[1, 2, 3, 4]
};

getProductOfAllElementsAtProperty(obj, 'key');
    
asked by SoniaGama 29.12.2017 в 09:41
source

2 answers

3

You should consider evaluating the number of elements in case of an Array, for example:

function getProductOfAllElementsAtProperty(obj, key) {
 var arr = obj[key];
 if(!Array.isArray(arr) || arr.length === 0){
    return 0;
 }
  else{
    var result = 1;
    for(var i = 0; i<arr.length; i++){
      result = result *= arr[i];      
    }
     return result;
  }
}

var obj = {key:[1, 2, 3, 4]};
console.log(getProductOfAllElementsAtProperty(obj, 'key'));

obj = {key:[]};
console.log(getProductOfAllElementsAtProperty(obj, 'key'));

obj = {key:"nombre"};
console.log(getProductOfAllElementsAtProperty(obj, 'key'));
    
answered by 29.12.2017 / 09:49
source
0

I guess your question is why when the array is empty it does not meet the condition arr === [] .

In javascript there are the types of values "primitive" or "types by value". This implies that these values are immutable and that when the value of a variable with a "primitive value" is accessed, the value is accessed directly. In this way, if two variables have the primitive value 3 and are compared, they will be equal since their values are the same:

var a = 3;
var b = 3;
var c = 4;
console.log('a y b tienen el valor 3 y son iguales:', a===b);
console.log('a y c tienen diferentes valores y son diferentes:', a===c);

All other types are objects or "types by reference". These values can be modified (you can add properties or methods, change the values of the properties, add or change elements of an array ...) and the variables what they contain is a reference to the object. In this way when comparing two variables with objects the values that are compared are the references (pointers) to the objects, that is, two variables will be the same if they have references to the same object, not if they refer to two identical objects:

var a = { prop: 4 };
var b = { prop: 4 };
var c = a;
console.log('a y b hacen referencia a diferentes objetos:', a===b);
console.log('a y c hacen referencia al mismo objeto: ', a===c);

We can see the difference if we change the value of the prop property of the current example in each of the objects:

var a = { prop: 4 };
var b = { prop: 4 };
var c = a;
console.log('a y b hacen referencia a diferentes objetos:', a===b);
console.log('a y c hacen referencia al mismo objeto: ', a===c);

a.prop = 5;
console.log('a.prop ahora es 5:', a.prop);
console.log('c.prop también es 5:', c.prop);
console.log('b hace referencia a otro objeto por lo que b.prop sigue siendo 4:', b.prop);

The same thing happens with arrays:

var a = [];
var b = [];
var c = a;
console.log('a y b hacen referencia a diferentes arrays (aunque los dos estén vacíos):', a===b);
console.log('a y c hacen referencia al mismo array: ', a===c);

I hope I have clarified it a bit.

    
answered by 29.12.2017 в 15:23