Skip to content

Instantly share code, notes, and snippets.

@yashasvi9199
Created July 25, 2023 09:44
Show Gist options
  • Save yashasvi9199/10f7a6478a479b032f012016536f48dd to your computer and use it in GitHub Desktop.
Save yashasvi9199/10f7a6478a479b032f012016536f48dd to your computer and use it in GitHub Desktop.
Create a function that takes a string of words and returns the highest scoring word. Each letter of a word scores points according to it's position in the alphabet: a = 1, b = 2, c = 3, etc.
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
</head>
<body>
<h3>Please enter a string !</h3>
</br>
<input type="text" id="input" placeholder="String Input">
</br>
<button id="Submit" onclick="val()">Check</button>
<input type="text" id="results" hidden>
<script>
var sum = [], // sum of characters in word
arr = []; //input array for whole string
function val() { //dunction to retreieve value of string
var input = $("#input").val(),
arr = input.split(" "),
largest = 0,
result = "";
for (var i = 0; i < arr.length; i++) {
var s = arr[i],
total = 0;
for (var j = 0; j < s.length; j++) {
var calc = s.toLowerCase().charCodeAt(j) - 97 + 1;
total += calc;
}
sum.push(total);
for (let k = 0; k < sum.length; k++) {
if (sum[k] > largest) {
largest = sum[k];
result = arr[i];
}
}
$("#results").removeAttr('hidden').val(result);
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment