codebuddies / DailyAlgorithms

Do a problem. Create (or find) your problem in the issues. Paste a link to your solution. See others' solutions of the same problem.
12 stars 1 forks source link

[CodeWars] Encrypt this! #25

Open rmorabia opened 5 years ago

rmorabia commented 5 years ago

Link: https://www.codewars.com/kata/encrypt-this/javascript

Encrypt this!

You want to create secret messages which can be deciphered by the Decipher this! kata. Here are the conditions:

  1. Your message is a string containing space separated words.
  2. You need to encrypt each word in the message using the following rules:
  3. The first letter needs to be converted to its ASCII code.
  4. The second letter needs to be switched with the last letter
  5. Keepin' it simple: There are no special characters in input.

Examples:

encryptThis("Hello") === "72olle"
encryptThis("good") === "103doo"
encryptThis("hello world") === "104olle 119drlo"
rmorabia commented 5 years ago
function encryptThis(string) {
  if (string.length === 0) {
    return false
  }
  const encryptedString = []
  const words = string.split(' ')
  for (i = 0; i < words.length; i++) {
    const word = words[i].split('')
    word.splice(0, 1, word[0].charCodeAt())
    word.splice(1, 1, words[i][word.length-1])
    word.splice(word.length-1, 1, words[i][1])
    encryptedString.push(word.join(''))
  }
  return encryptedString.join(' ')
}

encryptThis("Hello World")
encryptThis("Radhika Morabia")

Link: https://repl.it/@rmorabia/ScrawnyOffshoreDegree