title: How to find a highest scoring word inside a string?
body: <p>I am doing some of the CodeWars challenges again.</p>
  
  <p>I have a problem with this one:</p>
  
  <p>"Given a string of words, you need to find the highest scoring word.</p>
  
  <p>Each letter of a word scores points according to its position in the alphabet: a = 1, b = 2, c = 3 etc.</p>
  
  <p>You need to return the highest scoring word as a string.</p>
  
  <p>If two words score the same, return the word that appears earliest in the original string.</p>
  
  <p>All letters will be lowercase and all inputs will be valid."</p>
  
  <p>I've already looked up the solutions on SO, and have used the idea of one person(although I did change it a bit).</p>
  
  <p>It still does not work.</p>
  
  <p>Any ideas?</p>
  
  <pre><code>object Scoring {
  
  def high(s: String): String = {
  
          var max = 0
                  var whichWord = 0     
                  var x = 0
                  var y = new Array[Int](100)
  
  
                  for(word &lt;- s.split(" ")){
                      for(letter &lt;- word) letter match{
                      case 'a' =&gt; y(x)+=1
                      case 'b' =&gt; y(x)+=2
                      case 'c' =&gt; y(x)+=3
                      case 'd' =&gt; y(x)+=4
                      case 'e' =&gt; y(x)+=5
                      case 'f' =&gt; y(x)+=6
                      case 'g' =&gt; y(x)+=7
                      case 'h' =&gt; y(x)+=8
                      case 'i' =&gt; y(x)+=9
                      case 'j' =&gt; y(x)+=10
                      case 'k' =&gt; y(x)+=11
                      case 'l' =&gt; y(x)+=12
                      case 'm' =&gt; y(x)+=13
                      case 'n' =&gt; y(x)+=14
                      case 'o' =&gt; y(x)+=15
                      case 'p' =&gt; y(x)+=16
                      case 'q' =&gt; y(x)+=17
                      case 'r' =&gt; y(x)+=18
                      case 's' =&gt; y(x)+=19
                      case 't' =&gt; y(x)+=20
                      case 'u' =&gt; y(x)+=21
                      case 'v' =&gt; y(x)+=22
                      case 'w' =&gt; y(x)+=23
                      case 'x' =&gt; y(x)+=24
                      case 'y' =&gt; y(x)+=25
                      case 'z' =&gt; y(x)+=26
  
                      }
                      x +=1
                  }
  
          for(x &lt;- 0 until y.length){
  
              if(y(x) &gt; max)
              { 
                  max = y(x)
                          whichWord = x
              }
          }
  
          s.substring(whichWord-1, whichWord)
  }
  }
  </code></pre>
  
  <p>Here are the tests:</p>
  
  <p>test Results:</p>
  
  <pre><code>  RandomTestSpec
       high("ykvhorsqve kfkq jhjibercdptf efevxax ccr vnsmumqby jwhxvamegupfcj lierziuopbcsutm") should return "lierziuopbcsutm"
      Test Failed
  
      "[s]" was not equal to "[lierziuopbcsutm]"
  
   Stack Trace
  
      Completed in 34ms
       high("skwwwm") should return "skwwwm"
  
  Test Failed
  
  String index out of range: -1
   Stack Trace
  Completed in 1ms
   high("a") should return "a"
  Test Failed
  
  String index out of range: -1
   Stack Trace
  Completed in 1ms
   high("gykoialocufuc wcdwuxksqk bvapztcnqwx") should return "bvapztcnqwx"
  Test Failed
  
  "[y]" was not equal to "[bvapztcnqwx]"
   Stack Trace
  Completed in 1ms
   high("gdhfoonwtih xbvsiaqhsesl obrndpz nxt inkklyo lf oyoadxqhuys lbqr oxbqq bopalqknjxvpg") should return "oyoadxqhuys"
  Test Failed
  
  "o[]" was not equal to "o[yoadxqhuys]"
   Stack Trace
  Completed in 1ms
   high("bbzlmqhsypato pfufsi ryu oboklfa iigha h m") should return "bbzlmqhsypato"
  Test Failed
  
  String index out of range: -1
   Stack Trace
  Completed in 1ms
   high("dbtfwvhk kadarmvldjhkx dgxffryldcxodtn hoffibiayxriqe gtqzeuywpgc nqlgvudy") should return "dgxffryldcxodtn"
  Test Failed
  
  "[b]" was not equal to "[dgxffryldcxodtn]"
   Stack Trace
  Completed in 1ms
   high("vhyxdefryeznlkz fcaenzsnoxsn phdqu zjbbbybjmdn dbfhvxwnusz dqbqskfbwuomzsl ogsdioilk") should return "vhyxdefryeznlkz"
  Test Failed
  
  String index out of range: -1
   Stack Trace
   high("yqbzfuprmezbgee yxsewucg u") should return "yqbzfuprmezbgee"
  Test Failed
  String index out of range: -1
   Stack Trace
  Completed in 1ms
   high("zifha") should return "zifha"
  Test Failed
  
  String index out of range: -1
   Stack Trace
   high("moroydyolj tcfpokvitzwzor rnzeacau") should return "tcfpokvitzwzor"
  Test Failed
  
  "[m]" was not equal to "[tcfpokvitzwzor]"
   Stack Trace
  Completed in 1ms
   high("jhieih m") should return "jhieih"
  Test Failed
  
  String index out of range: -1
   Stack Trace
   high("yn ounbzw wk eldpjyikbfs nzm") should return "eldpjyikbfs"
  Test Failed
  
  "[ ]" was not equal to "[eldpjyikbfs]"
   Stack Trace
  Completed in 1ms
  </code></pre>