Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeWars] Encrypt this! #25

Open
rmorabia opened this issue Feb 26, 2019 · 1 comment
Open

[CodeWars] Encrypt this! #25

rmorabia opened this issue Feb 26, 2019 · 1 comment

Comments

@rmorabia
Copy link

rmorabia commented Feb 26, 2019

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
Copy link
Author

rmorabia commented Feb 26, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant