morse code
convert string to array. to object. Object
by trentHarlem
CSS
html {
background-color: #ccc;
}
JavaScript
let sample = 'A .- B -... C -.-. D -.. E . F ..-. G --. H .... I .. J .--- K -.- L .-.. M -- N -. O --- P .--. Q --.- R .-. S ... T - U ..- V ...- W .-- X -..- Y -.-- Z --.. 0 ----- 1 .---- 2 ..--- 3 ...-- 4 ....- 5 ..... 6 -.... 7 --... 8 ---.. 9 ----.'
const sampleMOD = sample.split(' ')
//console.log(sampleMOD)
const alpha = /[\w]/gi
const reg = /[\W]/gi
// const morseObj = {}
const alphabet = sampleMOD.filter(v => v.match(alpha))
const morse = sampleMOD.filter(v => v.match(reg))
console.log(alphabet)
console.log(morse)
// Object.keys(morseObj)
//. .splice()
// array.splice ( targetIndex, remove#, items)
// param 1 where to splice
// param 2 amount of indexes to remove
// param 3 items to add
//const morseObj = sampleMOD.reduce((a, c, i, arr) => {
// (i % 0 === 0) ? a.c: a[arr[i - 1]]=c;
//(i % 0 === 0) ? a.c=arr[i + 1]: a[arr[i - 1]] = c;
//}, {})
//console.log(morseObj)
//. FOR LOOP with i = +2 return every two items in array //string.split. or create 2 arrays / keys values
function makeObj(string) {
const stringArr = string.split(' ')
const alphabet = stringArr.filter(v => v.match(alpha))
const morse = stringArr.filter(v => v.match(reg))
const morseMap = alphabet.map((k, i) => {
return [k, morse[i]]
})
console.log(morseMap)
const morseObj = Object.fromEntries(morseMap)
console.log(morseObj)
return morseObj
}
const morseCode = makeObj(sample)
// console.log(morseCode.H)
function createMorse(str) {
/* const morseCode = makeObj(sample) */
//console.log('H', morseCode.H) // , morseCode['H'] )
//return str.toUpperCase().split('').map(c=>c==' '?' ' :morseCode[c]).join('')
// need to use BRACKET notation to search the variable
return str.toUpperCase().replace(/[\w]/gi, c => morseCode[c] + ' ')
// should a space exist between to chars??
}
//console.log(createMorse('H'))
console.log(createMorse('Hello World!'))
function translateMorse(str) {
// 52752000 //0827
const getKeyByVal = (object, value) =>...