# Discussion on: Daily Challenge #217 - SMS w/ an Old Phone

Matt Lott

JS Solution

``````const sendMessage = (msg) => {
const keys = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '*', '0', '#']
const letters = ['.,?!', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz', '-+=']
const letterKeyMap = {}
letters.forEach((ls, i) => ls.split('').forEach((l, r) => letterKeyMap[l] = [i, r + 1]))
const isLowerCase = (c) => c.toLowerCase() === c

let caseUp = false
let lastKey
return msg.split('').map((c, i, a) => {
let caseChange = ''
let key = ''
let extra = ''
if (c === ' ') { // Space
key = '0'
} else if (keys.indexOf(c) >= 0) { // Number
key = keys[keys.indexOf(c)]
extra = `-`
} else { // Letter
if (caseUp && isLowerCase(c) || !caseUp && !isLowerCase(c)) {
caseChange = '#'
caseUp = !isLowerCase(c)
}
const [ki, repeat] = letterKeyMap[c.toLowerCase()]
key = keys[ki]
extra = key.repeat(repeat - 1)
}
const addSpace = key === lastKey && caseChange === '' ? ' ' : ''
const res = `\${addSpace}\${caseChange}\${key}\${extra}`
lastKey = res[res.length - 1]
return res
}).join('')
}

const test = (msg, expected) => {
const result = sendMessage(msg)
console.log(`\${result.localeCompare(expected) === 0} \${msg} \${result} === \${expected}`)
}

test('Def Con 1!', '#3#33 3330#222#666 6601-1111')
test('hey', '4433999')
test('one two three', '666 6633089666084477733 33')
test('Hello World!', '#44#33555 5556660#9#66677755531111')
test('Hi dev.to!  Fun times.', '#44#44403 338881866611110 0#333#88660844463377771')
``````