Code to return up to 3 values in a specific format

Scenario:
A particular business card design can only accommodate up to three phone numbers.

Assume the phone numbers are always formatted as 10 digits with dashes. For example:
   Mobile: 456-789-1234
   Office: 987-654-3210
   Fax: 345-678-9012

There are only 2 lines to accommodate 3 phone numbers.

If (and only if) all 3 phone numbers contain a value, then the mobile and office numbers must appear on the first line, and the fax must appear on the second line, like this:
Mobile: 456-789-1234    Office: 987-654-3210
Fax: 345-678-9012

If (and only if) one of the 3 phone numbers does not contain a value, then the remaining 2 numbers must appear on their own line, like this:
Mobile: 456-789-1234
Fax: 345-678-9012

The example above assumes the Office number is the empty field. But if the Mobile number was the empty field then it would look like this:
Office: 987-654-3210
Fax: 345-678-9012

And if the Fax number was the empty field, then it would look like this:
Mobile: 456-789-1234
Office: 987-654-3210

If 2 of the 3 numbers are empty, then return just the one remaining number.

So given all of the possible phone number combinations, we have:
M   O   F
M   O
M       F
M
    O   F
    O
        F

Open in new window


I've managed to write JavaScript code using a bunch of if/then blocks, but I am sure there is an easier way to do it.
What is the most efficient code block that can return up to 3 phone numbers but follow the format rules above?
(Assume the 3rd number cannot just wrap to the second line)

I'll be writing this in JavaScript but pseudocode will be sufficient.
Thanks,
LVL 11
ecarboneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technology NinjaCommented:
Hi ecarbone,

If you can share your existing JavaScript I will be more than happy to modify it to address your query. Even if it is partially written, it will help me help you.

Regards,
Chinmay.
Jim RiddlesPrepress/OMS SpecialistCommented:
As Chinmay said, showing your code is a great first step.  Looking at your example, it basically boils down to the following order: mobile, office, and fax.  I'm assuming variable names with the example below, but it should be easy to follow.

if (m) {
  //mobile has a value, output the line
  document.write('Mobile: ' + m);
  //if office and fax have a value, output 4 spaces, otherwise output a line break
  if (o && f) {
  	document.write('    ');
  } else {
  	document.write('<br>');
  }
}
if (o) {
  //office has a value, output the line with a line break
	document.write('Office: ' + o + '<br>');
}
if (f) {
  //fax has a value, output the line
  document.write('Fax:&nbsp;' + f);
}

Open in new window


The code has comments explaining the solution.  Let me know if you have any questions.
Zakaria AcharkiAnalyst DeveloperCommented:
Hi ecarbone,

Check this function ;

function printPhoneNumbers(M, O, F) {
   var counter = Number(M.length > 0) + Number(O.length > 0) + Number(F.length > 0);
	var result = '';
  
	if( counter > 0 ){
    switch (counter) {
      case 3:
          result = 'Mobile: '+M+' &nbsp; &nbsp; Office: '+O+'<br>Fax: '+F;
        break;
      case 2:
      case 1:
          result += M.length > 0?'Mobile: '+M+'<br>':'';
          result += O.length > 0?'Office: '+O+'<br>':'';
          result += F.length > 0?'Fax: '+F:'';
          
        break;
    }
  }else{
  	alert('Pass at least one phone number.');
  }
  
  return result;
}

Open in new window


Here is a live fiddle to play with
Maximize Customer Retention with Superior Service

The IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more to help build customer satisfaction and retention.

Jim RiddlesPrepress/OMS SpecialistCommented:
@Zakaria Acharki, your solution does not address if there are only two numbers with a value.
Zakaria AcharkiAnalyst DeveloperCommented:
It does, have you tried the live fiddle?

The function accept three arguments as you can see in the definition :

printPhoneNumbers(M, O, F)

Open in new window


So you could set the ones you want and empty for the ones you don't want, try the following :

printPhoneNumbers('987-654-3210', '987-654-3211', ''); 
printPhoneNumbers('987-654-3211', '', '987-654-3210'); 
printPhoneNumbers('', '987-654-3211', '987-654-3210'); 

Open in new window

Jim RiddlesPrepress/OMS SpecialistCommented:
Ah, I see why.  You do not have a break after case 2: which allows it to continue to the next case.  A little confusing, at first glance.    I probably would have written it as follows to avoid any confusion:
function printPhoneNumbers(M, O, F) {
  var counter = Number(M.length > 0) + Number(O.length > 0) + Number(F.length > 0);
  var result;
  if (counter > 0) {
    switch (counter) {
      case 3:
        result = 'Mobile: ' + M + '&nbsp;&nbsp;&nbsp;&nbsp;Office: ' + O + '<br>Fax: ' + F;
        break;
      default:
        result = M.length > 0 ? 'Mobile: ' + M + '<br>' : '';
        result = O.length > 0 ? 'Office: ' + O + '<br>' : '';
        result = F.length > 0 ? 'Fax: ' + F : '';
        break;
    }
  } else {
      alert('Pass at least one phone number.');
  }
  return result;
}

Open in new window

Zakaria AcharkiAnalyst DeveloperCommented:
Ah, I see now, you're talking about the case 2 without break that called fall-through it's a feature of the switch statement, gives you the ability of grouping cases.

I use it instead of default here just for the precision and accuracy.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.