Skip to content

Instantly share code, notes, and snippets.

@aifarfa
Created May 9, 2015 13:06
Show Gist options
  • Save aifarfa/bd81e6ea525f7744c97a to your computer and use it in GitHub Desktop.
Save aifarfa/bd81e6ea525f7744c97a to your computer and use it in GitHub Desktop.
alphabet to number

alphabet to number

sum of "attitude" alphabet to number are equals 100? just for fun..

A Pen by Pasit R. on CodePen.

License.

<div role="dialog" ng-app="Attitude">
<h2>attitude 100%</h2>
<div ng-controller="TestCtrl">
{{itWork}}
<div>
<input type="text" ng-model="word" ng-change="doIt()" />
value: {{wordValue}}
</div>
</div>
</div>
angular.module('Attitude', []).
controller('TestCtrl', ['$scope', function($scope) {
$scope.itWork = 'Try another word';
$scope.alphabets = getAlphabet();
$scope.word = 'Attitude';
$scope.doIt = function() {
//if(!$scope.word.length) return;
var word = $scope.word.toLowerCase();
$scope.wordValue = sumOfWordValue(word);
};
$scope.doIt();
function sumOfWordValue(word) {
var result = 0;
for (var i = 0; i < word.length; i++) {
result += alphabetToNumber(word[i]);
}
return result;
}
function alphabetToNumber(char) {
return $scope.alphabets.indexOf(char) + 1;
}
function getAlphabet() {
var result = [];
for (var i = 97; i < 123; i++) {
var char = String.fromCharCode(i)
result.push(char)
}
return result;
}
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment