Skip to content

Instantly share code, notes, and snippets.

@AlixShahid
Created September 30, 2021 13:04
Show Gist options
  • Select an option

  • Save AlixShahid/b9eae08c1d6d35e80c7760c2270bcf8a to your computer and use it in GitHub Desktop.

Select an option

Save AlixShahid/b9eae08c1d6d35e80c7760c2270bcf8a to your computer and use it in GitHub Desktop.
Mentoring challenge 2, by McLarenCollege
function decipherThis(string) {
string_arr = string.split(' ') // [ "this", "is", "string" ]
result_array = []
for (i=0; i<string_arr.length; i++) {
word = string_arr[i]
// dealing with char Code
code = []
word_new = []
for (j=0; j<word.length; j++) {
if ( '0123456789'.includes(word[j]) ) {
code.push(word[j])
} else {
word_new.push(word[j])
}
}
ascii_code = Number(code.join(""))
first_char = String.fromCharCode(ascii_code)
filtred_char = word_new.join("")
WORD = first_char + filtred_char
// end dealing with char code
// dealing with position swaping
nth_1 = WORD[0]
nth_2 = WORD[1]
nth_last = WORD.slice(-1)
swaped_word = nth_1 + nth_last + WORD.slice(2, -1) + nth_2
if ( WORD.length < 3 ) {
swaped_word = nth_1 + nth_last + WORD.slice(2, -1)
}
result_array.push(swaped_word)
// end dealing with position swaping
}
return result_array.join(" ")
}
test1 = decipherThis('72olle 103doo 100ya'); // 'Hello good day'
test2 = decipherThis('82yade 115te 103o'); // 'Ready set go'
console.log(test1)
console.log(test2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment