The question is simple. I did something wrong but I can't find the solution. Am I missing something?
function harveyScrabble($mot){
$worth10Points = array("y","Y");
$worth5Points = array("v","V");
$worth3Points = array("h","H");
$worth2Points = array("r","R");
$worth1Point = array("a","A","b","B","c","C","d","D","e","E","f","F","g","G","i",
"I","j","J","k","K","l","L","m","M","n","N","o","O","p","P",
"q","Q","s","S","t","T","u","U","w","W","x","X","z","Z");
$total = 0;
for ($i=0; $i <count($mot) ; $i++) {
$char = $mot{i};
if (in_array($char, $worth10Points)) {
$total += 10;
}
elseif (in_array($char, $worth5Points)) {
$total += 5;
}
elseif (in_array($char, $worth3Points)) {
$total += 3;
}
elseif (in_array($char, $worth2Points)) {
$total += 2;
}
else{
$total += 1;
}
}
return $total;
}
When I try to called the function with the word "Harambe" it gives me only 3 points.
You need to change
for ($i=0; $i <count($mot) ; $i++) {
to
for ($i=0; $i <strlen($mot) ; $i++) {
Count is for counting elements in an array (or countable item in an object), strlen will count the number of characters in a string.
You could also loop trough the word like so:
foreach(str_split($mot) as $letter){
echo "$letter ";
}
Also you should play around with arrays a bit more. If you learn how to use them you can shorten the code significantly.
function harveyScrabble($word){
$word = strtolower($word);
$total = 0;
$points = ['y' => 10, 'v' => 5, 'h' => 3, 'r' => 2];
foreach(str_split($word) as $letter){
if(isset($p = $points[$letter])){
$total += $p;
} else {
$total += 1;
}
}
return $points;
}