DEV Community

Discussion on: Daily Challenge #110 - Love VS. Friendship

Collapse
 
lloricode profile image
Lloric Mayuga Garcia • Edited

In PHP


$alpha = function (string $string) {
    $al = array_flip(range('a', 'z'));

    $points = 0;
    foreach (str_split($string) as $s) {
        $points += $al[$s] + 1;
    }
    return $points;
};

echo $alpha('friendship');

EDIT:

array_map(function (string $string) {
    $al = array_flip(range('a', 'z'));

    $points = 0;
    foreach (str_split($string) as $s) {
        $points += $al[$s] + 1;
    }
    echo "$string: $points<br>";
}, ['attitude', 'friends', 'family', 'selflessness', 'knowledge']);

attitude: 100
friends: 75
family: 66
selflessness: 154
knowledge: 96