JSFiddle - React, Tailwind, and code Playground
JavaScript
var s = 'jgtgkuvjghkpcnvguvecugvjcvaqwowuvfgetarv',
alphabet = 'abcdefghijklmnopqrstuvwxyz',
alphabetFrequency = 'e t a o i n s h r d l c u m w f g y p b v k j x q z'.split(' '),
scores = {}
for (var i = 0; i < alphabet.length; i++) {
scores[i] = getScore(rot(s, alphabet.length - i))
}
var minScore = Infinity, minScoreN = -1
for (var x in scores) {
if (scores[x] < minScore) minScore = scores[x], minScoreN = x
}
console.log((minScoreN + 1) + ' ' + rot(s, alphabet.length - minScoreN))
function getScore(str) {
var score = 0
for (var i = 0; i < str.length; i++) {
score += alphabetFrequency.indexOf(str.charAt(i))
}
return score
}
function rot(str, amount) {
var rotated = ''
for (var i = 0; i < str.length; i++) {
rotated += alphabet[(alphabet.indexOf(str.charAt(i)) + amount) % alphabet.length]
}
return rotated
}