Skip to content

Instantly share code, notes, and snippets.

@waswrongassembled
Forked from 140bytes/LICENSE.txt
Last active December 15, 2015 12:59
Show Gist options
  • Save waswrongassembled/5263985 to your computer and use it in GitHub Desktop.
Save waswrongassembled/5263985 to your computer and use it in GitHub Desktop.

simple word value

Calculates a simple word (or letter) value in 112 bytes.

The word (or letter) value is a well known geocaching method to turn words or characters into numbers. A value is assigned to each character and those values are summed and sometimes summed again to end up with only one digit, the latter is sometime calles the row sum method. There are multiple ways to assign values to characters, the default assignment is A=1, B=2, C=3, ..., Z=26.

Benchmarks: [http://jsperf.com/cachedarraycreation/2]

function(a){
for(
b=0,c=a.length-1; //declaring vars
b+='abcdefghijklmnopqrstuvwxyz'.indexOf(a[c].toLowerCase())+1,c--;); //steeping through word and sum index
return b
};
function(a){for(b=0,c=a.length-1;b+='abcdefghijklmnopqrstuvwxyz'.indexOf(a[c].toLowerCase())+1,c--;);return b};
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 André Schade <waswrongassembled@mac.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "simpleWordValue",
"description": "The word (or letter) value is a well known geocaching method to turn words or characters into numbers.",
"keywords": [
"string",
"geocaching",
"140bytes"
]
}
<!DOCTYPE html>
<title>test simple word value</title>
<div>Expected value: <b>430</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
var simpleWordValue = function(a){for(b=0,c=a.length-1;b+='abcdefghijklmnopqrstuvwxyz'.indexOf(a[c].toLowerCase())+1,c--;);return b};
document.getElementById( "ret" ).innerHTML = simpleWordValue('0. You just DO WHAT THE FUCK YOU WANT TO.');
</script>
@atk
Copy link

atk commented Apr 12, 2013

This leaks b and c into the global scope. Smaller version:

function(a,b,c){b=0;for(c in a)b+='abcdefghijklmnopqrstuvwxyz'.indexOf(a[c].toLowerCase())+1;return b}

Even smaller:

function(a,b,c,d){b=0;for(c in a)d=a.toLowerCase().charCodeAt(c)-96,b+=d<0^d>26?0:d;return b}

@openorclose
Copy link

Even smaller:D

function b(a){return!!a&&Math.max(0,~~parseInt(a[0],36)-9)+b(a.slice(1))}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment