-6

In the below function, I was expecting two values to be returned but I am getting only one value in the console.

var contacts =[
            {"firstname": "karthi","lastname": "kosuri","mobile": "9999666888889","likes":["pizza","icecream","curdrice"]},
            {"firstname": "sathvik","lastname": "kosuri","mobile": "9849488486","likes":["biryani","vada","idly"]},
            {"firstname": "neelu","lastname": "kosuri","mobile": "892736636","likes":["annam","pappu","charu"]},
            {"firstname": "kesav","lastname": "kosuri","mobile": "748484848484","likes":["kudithi","sambar","bokka"]}
        ];

    function contactlookup(name, prop){
        for( var i=0; i< contacts.length;i++){
            if(contacts[i].firstname=== name){
                return contacts[i][prop];
            }
        }
        return "no such name";
    }
    var data = contactlookup("karthi", "kosuri");
    console.log(data); 
jlewkovich
  • 2,725
  • 2
  • 35
  • 49
surap.cop
  • 23
  • 1
  • 4
  • Do you mean you expected `return name && prop` to return both `name` *and* `prop`? That's **not** what that means; `x && y` is effectively `x ? y : x`, it will only resolve to *one* of those values. – jonrsharpe Jan 11 '20 at 13:14

3 Answers3

0

You can only return one value.

return name && prop;

This returns the result of evaluating name && prop (see Logical operators in JavaScript — how do you use them?) which will be either the value of name (if name is falsey) or prop.

The closest you can come to returning multiple values is to put them in a data structure and return that data structure.

e.g. an array:

return [name, prop];

or an object:

return { name, prop };
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Use "return {name:name,prop:prop};" instead of "return name && prop" Because one value should be return at the time of function call

var contacts =[
    {
        "firstname": "karthi",
        "lastname": "kosuri",
        "mobile": "9999666888889",
        "likes":["pizza","icecream","curdrice"]
    },
    {
        "firstname": "sathvik",
        "lastname": "kosuri",
        "mobile": "9849488486",
        "likes":["biryani","vada","idly"] 
    },
    {
        "firstname": "neelu",
        "lastname": "kosuri",
        "mobile": "892736636",
        "likes":["annam","pappu","charu"]
    },
    {
    "firstname": "kesav",
    "lastname": "kosuri",
    "mobile": "748484848484",
    "likes":["kudithi","sambar","bokka"]
    }
];
// newcontact = JSON.stringify(contacts);
// console.log(newcontact)
function contactlookup(name, prop){
    for( var i=0; i< contacts.length;i++){
if(contacts[i].firstname=== name && contacts[i].lastname=== prop){
    **return {name:name,prop:prop}**
}

    }
return "no such name";

}
var data = contactlookup("karthi", "kosuri");
console.log(data);
Soura Ghosh
  • 879
  • 1
  • 9
  • 16
0

use array find https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

var contacts = 
    [ { firstname: 'karthi'
      , lastname:  'kosuri'
      , mobile:    '9999666888889'
      , likes:     [ 'pizza', 'icecream', 'curdrice' ] 
      } 
    , { firstname: 'sathvik'
      , lastname:  'kosuri'
      , mobile:    '9849488486'
      , likes:     [ 'biryani', 'vada', 'idly' ] 
      } 
    , { firstname: 'neelu'
      , lastname:  'kosuri'
      , mobile:    '892736636'
      , likes:     [ 'annam', 'pappu', 'charu'] 
      } 
    , { firstname: 'kesav'
      , lastname:  'kosuri'
      , mobile:    '748484848484'
      , likes:     [ 'kudithi', 'sambar', 'bokka'] 
      } 
    ];

function contactlookup(name, prop)
{
  let lookup = contacts.find(elm=>elm.firstname===name && elm.lastname===prop )
  return (lookup)? lookup : 'no such name' 
}


var data = contactlookup('karthi', 'kosuri')

console.log(data); 
.as-console-wrapper { max-height: 100% !important; top: 0; }
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40