Deevinee Deevinee - 7 months ago 10
Javascript Question

javascript func to check for a number and return

I need to write a javascript function "Determine" to check and return a number with the following logic.

Could someone please help me.

$scope.doctor[index].number = Determine(mob, home);

Function Determine(mob, home) {
if (mob!=null)
return mob;
else if (home!=null)
return home;
}


I am little new to js and learning, please help me for the above algorithm that could return to the $scope field.

Answer

var doctor = {};

doctor['A'] = {
  'mob': '1234567',
  'home': '7654321',
  'number': ''
};

doctor['B'] = {
  'mob': null,
  'home': '7654321',
  'number': ''
};

doctor['C'] = {
  'mob': '1234567',
  'home': null,
  'number': ''
};

doctor['D'] = {
  'mob': null,
  'home': null,
  'number': ''
};


$.each(doctor, function(key, val){
  val.number = determine(val.mob, val.home);
});

function determine(mob, home){
  var result = null;
  if (mob != null){
    result = mob;
  }
  else if (home != null){
    result = home;
  }
  return result;
}
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
    <script src="my.js"></script>
  </head>
  <body>
  </body>
</html>