Solving Popular Algorithms: Replace “?”s

Cute doggy + ?s
let getRandomChar = (prev, nxt) => {
let alphaArr = ['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 randomChar = alphaArr[Math.floor(Math.random() * alphaArr.length)]
while (randomChar === prev || randomChar === nxt) {
randomChar = alphaArr[Math.floor(Math.random() * alphaArr.length)]
}

return randomChar
}
var modifyString = function(s) {

let splitS = s.split("")

for (let i = 0; i < splitS.length; i++) {
if (splitS[i] === '?' && i > 0) {
let prev = splitS[i-1]
let nxt = splitS[i+1]
let newChar = getRandomChar(prev, nxt)
splitS.splice(i, 1, newChar)
} else if (splitS[i] === '?') {
let nxt = splitS[i+1]
let newChar = getRandomChar("1", nxt)
splitS.splice(i, 1, newChar)
} else if (splitS[i] === '?' && !splitS[i+1]) {
let prev = splitS[i-1]
let newChar = getRandomChar(prev, "1")
splitS.splice(i, 1, newChar)
}
}

return splitS.join("")

};
Cute doggy 2 + ?s

--

--

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