-2

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.

Xorifelse
  • 7,878
  • 1
  • 27
  • 38
  • 1
    *The question is **too** simple* ... Whats the actual problem? – IncredibleHat Mar 08 '18 at 19:24
  • 3
    `$char = $mot[$i];`, now read a book. Also [mcve] and [ask]. – Xorifelse Mar 08 '18 at 19:25
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. – Kevin Kopf Mar 08 '18 at 19:26
  • Are you expecting that we magicly know what you're talking about? You're posting code and expect us to tell you what's wrong with it. No explanation on what the code does or doesn't or what it's supposed to do.. – icecub Mar 08 '18 at 19:26
  • The question is when I try to called the function , I write example the word "Harambe" it gives me only 3 points. I dont understand even when I changed to $char = $mot[$i]; This code is suppose to calculate the numbers of points for each letters in a scrabble game. As for the example I used the word Harambe. – Engjëll Bislimi Mar 08 '18 at 19:27
  • Suggestion: Uppercase and lower care doesn't matter, do a `$mot = strtolower($mot);` in the beginning of the function. Then you can remove all the uppercase letters from the arrays. Solution: in your for loop, change `count()` to `strlen()`. Assuming `$mot` is just a string. – Xorifelse Mar 08 '18 at 19:32
  • Try `strlen($mot)` instead of `count($mot)` – aynber Mar 08 '18 at 19:34
  • thank you. Sorry for the plain question at the beginning. – Engjëll Bislimi Mar 08 '18 at 19:55
  • Please explain the expected result. E.g. `Scrabble function, input is Y instead of X` – adrian7 Mar 08 '18 at 20:34

2 Answers2

0

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.

aynber
  • 22,380
  • 8
  • 50
  • 63
  • 1
    Additionally, the individual letters can not be accessed as `$mot{i}` but should be `$mot[$i]` – Erik Mar 08 '18 at 19:56
0

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;
}
Xorifelse
  • 7,878
  • 1
  • 27
  • 38