CoDEVIANT #20 (10/7/20)-Strings and Things (Soup is good food)

I don’t know who wouldn’t know that, but…just in case.
function isPalindrome(string) {
if(string.length === 1) {
return true
}
string.split('')
let left = 0
let right = string.length - 1
while(left < string.length && left !== right) {
if(string[left] == string[right]) {
left++
right--
} else {
return false
}
}
return true
}
if(string.length === 1) {
return true
}
string.split('')
let left = 0
let right = string.length - 1
  • We want left to not be longer than the array itself, otherwise that would be silly and we’d go on to infinity and your computer would crash.
  • We want left not equaling right because if it does, then they are pointing at the same element in the array which would mean they are at the one unique value in the potential palindrome
while(left < string.length && left !== right) {
...
}
while(left < string.length && left !== right) {
if(string[left] == string[right]){
left++
right--
} else {
return false
}
}
return true
Once again for the kids in the back…
function isPalindrome(string) {
if(string.length === 1) {
return true
}
string.split('')
let left = 0
let right = string.length - 1
while(left < string.length && left !== right) {
if(string[left] == string[right]) {
left++
right--
} else {
return false
}
}
return true
}
function isPalindrome(string) {
let reversedString = ''
for(let i = string.length - 1; i >=0; i--){
reversedString += string[i]
}
return string === reversedString
}
Touché
let reversedString = ''
  • makes the iterator (i) equal the length of the string minus one
  • iterates over the length of the string
  • stays active as long as the iterator (i) is more than or equal to 0
  • in each loop decrements the iterator (i)
for(let i = string.length - 1; i >= 0; i--) {
...
}
for(let i = string.length - 1; i >= 0; i--) {
reversedString += string[i]
}
return string === reversedString
Good show, nerds.
function caesarCipherEncryptor(string, key) {
let alphabet = ['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']
let arr = []
string = string.split('')
string.forEach(el => {
alphabet.forEach((letter, index) => {
if(el === letter) {
let num = index + key
while(num > 25 ) {
num = Math.abs(alphabet.length - num)
}
arr.push(num)
}
})
})
return arr.map(el => {
return alphabet[el]
}).join('')
}
Let’s rock.
;)
let alphabet = ['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']
let arr = []
string = string.split('')
string.forEach(el => {
...
})
string.forEach(el => {
alphabet.forEach((letter, index) => {
...
})

})
string.forEach(el => {
alphabet.forEach((letter, index) => {
if(el === letter) {
let num = index + key
}

})
})
string.forEach(el => {
alphabet.forEach((letter, index) => {
if(el === letter) {
let num = index + key
}
while(num > 25) {
num = Math.abs(alphabet.length - num)
}
arr.push(num)

})
})
Huh? What? HUH? WHAATT?
So we just do that for all of the letters in the string we get provided which we turn into an array. Point is, we are left with an array of numbers in ‘arr’.
return arr.map(el => {
return alphabet[el]
}).join('')
function caesarCipherEncryptor(string, key) {
let newLetters = []
let newKey = key % 26
for(const letter of string) {
newLetters.push(getNewLetter(letter, newKey))
}
return newLetters.join('')

function getNewLetter(letter, key) {
const newLetterCode = letter.charCodeAt() + key
return newLetterCode <= 122
? String.fromCharCode(newLetterCode)
: String.fromCharCode(96 + (newLetterCode % 122))
}
}
let newLetters = []
let newKey = key % 26
  • *This is a way of iterating over each character in a string without first turning it into an array. I wouldn’t have thought of it and it’s not something I’ve seen alot, but it’s cool to know about**
for(const letter of string) {
newLetters.push(getNewLetter(letter, newKey))
}
charCo…wha?
  • a in unicode is 97
  • z in unicode is 122
function getNewLetter(letter, key) {
const newLetterCode = letter.charCodeAt() + key
}
  • If newLetterCode is 122 or under, we return the result of String.fromCharCode(newLetterCode)
  • If newLetterCode is over 122, we return the result of String.fromCharCode(96 + (newLetterCode % 122))
function getNewLetter(letter, key) {
const newLetterCode = letter.charCodeAt() + key
return newLetterCode <= 122
? String.fromCharCode(newLetterCode)
: String.fromCharCode(96 + (newLetterCode & 122))

}
return newLetters.join('')
function caesarCipherEncryptor(string, key) {
let newLetters = []
let newKey = key % 26
for(const letter of string) {
newLetters.push(getNewLetter(letter, newKey))
}
return newLetters.join('')

function getNewLetter(letter, key) {
const newLetterCode = letter.charCodeAt() + key
return newLetterCode <= 122
? String.fromCharCode(newLetterCode)
: String.fromCharCode(96 + (newLetterCode % 122))
}
}
One things for sure, I’m expecting to find some weird shit in my soup from now on. Just tons of numbers and such.

--

--

--

is a web developer, opera singer, actor, and lover of cats. (adrian-rosales.tech)

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Next.Js : I think I am in Love ❤️

mangi! by dotsara on Flickr.

What is Babel-Js and it’s Configuration.

Watching tests — Mocha & Babel & React

Bonus 3: Rotate Array Clockwise

Why JavaScript Is A Good Choice For Beginners

Algorithm Practice

Recursion in JavaScript

Recursion in JavaScript

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adrian Rosales

Adrian Rosales

is a web developer, opera singer, actor, and lover of cats. (adrian-rosales.tech)

More from Medium

Callback, Promises & Async/Await The Difference!

Handling the Fetch Error (404) in JavaScript

What is the difference between Strategy, State, and Template design patterns?

Pair Programming