## javascript anagram detector

### Aside

what’s the shortest anagram detector you know how to write, in javascript?

there must be a shorter way to do this. highlight below to see a solution.

```
var anagramDetector = function (x,y) {
if (x.length != y.length) return false;
var alpha_x = x.split("").sort().join("");
var alpha_y = y.split("").sort().join("");
for ( j = 0; j<y.length;++j) {
if (alpha_x[j] != alpha_y[j]) return false;
}
return true;
}
```

PS: there’s a cool method for solving this for a fixed alphabet, using primes

`[More programming riddles]`

## detecting anagrams in python

### Aside

what’s the shortest anagram detector you know how to write, in python?

this is one that keeps feeling like it should be a one liner (because it’s very similar to this one liner), but i suspect can’t be done in one line

highlight to see a solution.

```
def fn(x,y):
if len(x) != len(y):
return False
for i in x:
y = y.replace(i,'',1)
return not len(y) > 0
```

PS: there’s a cool method for solving this for a fixed alphabet, using primes

`[More programming riddles]`

## arrays (javascript)

### Aside

how well do you know your arrays?

figure out what you think each line does, and then check yourself by running them in a console

```test = [1,2,3]

test
test.length
test.length = 5
test.length
test
```

`[More programming riddles]`

## riddle 11 (javascript)

### Aside

javascript riddle

does null == false?

```if (null)          { console.log("null"); }
if (null == false) { console.log("null == false"); }
if (!null)         { console.log("!null"); }
```

`[More programming riddles]`

## riddle 10 (javascript)

### Aside

javascript riddle #4

true or false?

```['a','b'] < ['a','b']
['a','b'] <= ['a','b']
['a','b'] == ['a','c']
['a','b'] >= ['a','c']
['a','b'] > ['a','c']

['a','b'] = ['a','b']
['a','b'] == ['a','b']
['a','b'] === ['a','b']
```

`[More programming riddles]`

## riddle 9 (python)

### Aside

python riddle

```def fn():
x = "Who's on first"
def f(y):
return x+y
x = "What's on second"
return f("?")

print fn()
```

(ok, so this is more of a mnemonic to help me remember how python works)

`[More programming riddles]`

## null vs. undefined (javascript)

### Aside

how well do you know your “null”s and “undefined”s?

write down what you expect the result of each operation to be. run them in a javascript console (ChromeFirefox/FirebugSafari). compare, verify, contrast, and, for fun, give yourself a score out of “3/4 donut”.

```// Equality
null == undefined
null === undefined
!null == !undefined
!null === !undefined

null + ''
undefined + ''
null + 1
undefined + 1

// fun with enumerations
test = []; test.push(null); console.log(test)
test = []; test.push(undefined); console.log(test)
{null:2}
{undefined:2}
{a:null}
{a:undefined}```

`[More programming riddles]`

## riddle 8 (javascript)

### Aside

javascript riddle

see if you can explain what exactly is going on here, and why

`7 > 5 > 3`

`[More programming riddles]`

## riddle 7 (python)

### Aside

python riddle

```def listMutator(arglist):
arglist = ["mutated"]
print "arglist: ",  arglist

test = ["not mutated"]
listMutator(test)
print "test: ", test```

Bonus question – when is the following statement True, and when is it False?

```-7/6 == -(7/6)
```

`[More programming riddles]`