Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: Code Challenge!

  1. #1
    Regular Coder
    Join Date
    Dec 2002
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Code Challenge Resurrected!

    In the fewest bytes of JavaScript code, how would you write a function that accepts a phone number and returns an array of phone words?

    function phoneWord(pn){/* your code here */}

    phoneWord(2255288) should return an array with 2187 (3*3*3*3*3*3*3) different items, one of which should be 'CALLATT'.

    Let's call this contest by 12/7/2004.

    Good Luck!

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    I really think it's better if this is a new thread instead of a years old one. Splitting.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    whats a phone word?

  • #4
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Took me a minute too, but still not sure. Do you mean the words that could be made, based on the letters assigned to the numbers of the phone number?

    Basscyst
    Helping to build a bigger box. - Adam Matthews

  • #5
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I would think a serverside method would be more effective due to the possible combinations.
    here is one that runs a serverside although closed source - http://www.phonespell.org/
    although it only find mnemonics.

  • #6
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Code:
    Array.prototype.map = function(func) {
    	if (this.length) return [func(this[0])].concat(this.slice(1).map(func));
    	return [];
    }
    function phoneWord(pn) {
    	return (function(func, running, arr) {
    			if (arr.length)
    				return arguments.callee(func, func(arr[0], running), arr.slice(1));
    			return running;
    		})(function(a1, a2) {
    			if (a1.length)
    				return a2.map(function(el) { return el+a1[0] }).concat(arguments.callee(a1.slice(1), a2));
    			return [];
    		}, [""], pn.toString().split("").map(parseInt).map(
    			function(button) {
    				if (button)
    					return [3*button+59, 3*button+60, 3*button+61].map(String.fromCharCode);
    				return ["Y","Z"];
    			}));
    }
    The map could be phased out but actually defining it instead of using anonymous lambda functions shortens the code, as the script relies on map in several areas. Also, the code could be compressed a good deal.
    Last edited by jkd; 11-05-2004 at 06:35 PM.

  • #7
    Regular Coder
    Join Date
    Dec 2002
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Excellent!

    Quote Originally Posted by jkd
    Code:
    Array.prototype.map = function(func) {
    	if (this.length) return [func(this[0])].concat(this.slice(1).map(func));
    	return [];
    }
    function phoneWord(pn) {
    	return (function(func, running, arr) {
    			if (arr.length)
    				return arguments.callee(func, func(arr[0], running), arr.slice(1));
    			return running;
    		})(function(a1, a2) {
    			if (a1.length)
    				return a2.map(function(el) { return el+a1[0] }).concat(arguments.callee(a1.slice(1), a2));
    			return [];
    		}, [""], pn.toString().split("").map(parseInt).map(
    			function(button) {
    				if (button)
    					return [3*button+59, 3*button+60, 3*button+61].map(String.fromCharCode);
    				return ["Y","Z"];
    			}));
    }
    The map could be phased out but actually defining it instead of using anonymous lambda functions shortens the code, as the script relies on map in several areas. Also, the code could be compressed a good deal.
    Excellent!

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Location
    Atlanta, GA.
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The function
    Code:
    <script>
    function phoneWord( num, str, result)
    {
    	var s  = ["0", "1", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ"][num.charAt(0)];
    	str    = str == undefined ? "" : str;
    	result = result == undefined ? new Array() : result;
    	for(var i=0 ; i<s.length ; i++)
    		if(num.length > 1)
    			phoneWord(num.slice(1) , str + s.charAt(i), result)
    		else
    			result[result.length] = str + s.charAt(i);
    	return result;
    }
    </script>
    The test
    Code:
    <script>
    var x=phoneWord("2255288");
    var y=phoneWord("769");
    
    document.write("<textarea rows=40 cols=100>" + x + "</textarea>");
    document.write("<textarea rows=3 cols=100>" + y + "</textarea>");
    document.write("<textarea rows=3 cols=100>" + y[34] + "," + x[1674] + "</textarea>");
    </script>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •