Code to return up to 3 values in a specific format

ecarbone
ecarbone used Ask the Experts™
on
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,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
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 Specialist

Commented:
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 Developer
Distinguished Expert 2018
Commented:
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
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Jim RiddlesPrepress/OMS Specialist

Commented:
@Zakaria Acharki, your solution does not address if there are only two numbers with a value.
Zakaria AcharkiAnalyst Developer
Distinguished Expert 2018

Commented:
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

Prepress/OMS Specialist
Commented:
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 Developer
Distinguished Expert 2018

Commented:
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.
Hi everyone. I'm going through my EE account and realize I never marked this question as answered. All of your responses have been awesome. Thank you!!
Big thanks! I like both proposed solutions.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial