Jump to content

Converting a Number to a letter


Sellis

Recommended Posts

Hello all I got some help with this 6 months or so ago, and it seems that something happened and it's not working anymore.

Here is what I need to achieve.

We are doing versioned cards for a school for the blind. The front is sighted text, and the back get's embossed with braille for dual use.

 

I got help getting a code that would allow the user to enter in their phone number, and it would convert it to US English 2 braille. (IE the numbers become letters.) it was working fine for a while, now it's no longer working. A note we do not use what get's generated for anything more than a proof, another vendor does the braille embossing.

 

Here is the variable, if they want the same phone in braille that is in phone 1 we allow them leave the braille phone line blank.

 

Here is the code I have been using.

This job resides on an EFI/Digital Store front site, and is allowed for one off use only. Any ides why I'm not getting the result i'm looking for?

 

Var1 = Field("Phone1")

Var2 = Field("Braille Phone")

 

if (Field("Braille Phone") =="")

 

return formatBraille(Field("Phone1")); // #617-555-1234

 

else

 

return formatBraille(Field("Braille Phone")); //#+617-555-1234

 

////

function formatBraille(number) {

number = number

.split('')

.map(function(s) { return /\d/.test(s) ? Chr(96 + Int(s)) : s; })

.join('')

.replace(/[^a-j]/gi, '')

.match(/^(.{3})?(.{3})(.{4})$/);

if (!number) return '';

number.shift();

return '#' + number.filter(Boolean).join('-');

}

 

 

Thank you

Sean

Link to comment
Share on other sites

Any ides why I'm not getting the result i'm looking for?

What is the result you're looking for? And what is the result you're actually getting, and exactly how is it different from the expected result? And precisely what data are you passing to the function that's generating this result?

 

If I put your function in a rule and call it like so:

return formatBraille("#+312-555-1234");

The result is:

#cab-eee-abcd

Is that not correct?

Link to comment
Share on other sites

  • 2 weeks later...
Hi Dan I figured out the issue finally.

The substitution is not catching 0. When there is a 0 in the phone number, the phone script fails.

Fails in what way? Again: What is the result you're looking for? And what is the result you're actually getting, and exactly how is it different from the expected result? It's hard to meet a requirement that's not well-defined.

 

In other words, what letter is a zero supposed to turn into?

 

I mean, the problem isn't that hard to find. You're splitting up the string into individual characters (the line ".split('')"), then adding the value of each digit to 96 and returning the ASCII character with that code (in the line of the function starting with ".map"). For the digits 1 through 9, adding 96 gives you 97 through 105, which are the ASCII codes for lower-case letters 'a' through 'i'. But if the digit is zero, you get 96, which is the ASCII code for "grave accent" (`). Then you're stripping out any character that's not lower-case letters 'a' through 'j' (in the line of the function starting with ".replace"), so those grave accent characters get removed. Then the entire result doesn't match the pattern of three digits, three digits, and four digits (in the line of the function starting with ".match"), so you get an empty/null result.

 

If you want the zero to turn into the letter 'j', then this should work:

function formatBraille(number) {
   number = number
           .split('')
           .map(function(s) { return s == '0' ? 'j' : /\d/.test(s) ? Chr(96 + Int(s)) : s; })
           .join('')
           .replace(/[^a-j]/gi, '')
           .match(/^(.{3})?(.{3})(.{4})$/);
   if (!number) return '';
   number.shift();
   return '#' + number.filter(Boolean).join('-'); 
}

Although I would do it slightly differently:

function formatBraille(input)
{
   var digits = String(input).replace(/\D/g,'');
   var letters = digits.split('').map(function(s) { return s == '0' ? 'j' : Chr(96 + Int(s)); }).join('');
   var r = letters.match(/^(.{3})?(.{3})(.{4})$/);
   return r ? "#" + r[1] + "-" + r[2] + "-" + r[3] : '';
}

I would also add a bit of error handling and reporting to the function, so that if it does fail, the end user has some idea what went wrong:

function formatBraille(input)
{
   var digits = String(input).replace(/\D/g,'');
   if (digits.length != 10)
       throw "Error: formatBraille requires exactly ten digits";

   var letters = digits.split('').map(function(s) { return s == '0' ? 'j' : Chr(96 + Int(s)); }).join('');
   var r = letters.match(/^(.{3})?(.{3})(.{4})$/);
   return r ? "#" + r[1] + "-" + r[2] + "-" + r[3] : '';
}

The other piece of advice I would add is about testing coverage. So, if you have a function that's supposed to do something specific with a specific set of characters, such as the digits 0 through 9, then you should test that entire set of characters. In this case, it's not hard to test all ten digits, like so:

return formatBraille("123-456-7890");

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...