Ad
  • Custom User Avatar

    I'm currently trying to solf the TVRemote(wrap). I already solved it in python but i've been trying to solve it in JS. When I call the main function with the exact same test words that fail, it logs the correct answer but the test says it is logging a different answer. I'm still very new to JS and can't figure out what else I can do since all of my logs come back correct. Any help is appreciated. My code is below. I called the first function with the first test that fails so you can see difference in what is logged.

    // each string in the array is a row and each character in each string is a column.
    const rows = ['abcde123','fghij456','klmno789',
                 'pqrst.@0','uvwxyz_/','*#&&&&&&'];
    
    
    let current = [0,0];
    let new_l = [];
    
    // function that finds column and row of a given letter or character.
    const find_letter = letter => {
      let count = 0;
      for (i=0; i<rows.length; i++){
        
        // if the character isn't in the row, move to the next row.
        const column = rows[i].indexOf(letter);
          if(column === -1){
            continue;
            
            // if it is found, update row and column to the new letter.
          } else {
            new_l.push(i);
            new_l.push(column);
            
            //gives the distance to between current position and new character.
            let row = Math.abs(current[0]-new_l[0]);
            let col = Math.abs(current[1]-new_l[1]);
            
            // used to represent the wrapp arround ability
            if(row > 3) {
              row = 6-row;
            } if(col > 4) {
              col = 8-col;
            }
            
            // update count and new current position. The +1 is the "click" of the button.
            count += row + col +1;
            current = new_l;
            new_l = [];
            break;
          }
          } 
      
            // returns how many clicks it took from the previous character to press the current character.
            return count ;
        };
    
    
    const tvRemote = function(words) {
      // current total count that will be returned
      let count = 0;
      
      // represents current state of keyboard. False = lowercase, True = upercase.
      let shift = false;
      
      //check each character in word/words.
      [...words].forEach(l => {
        
        // if there is a space, the button to press is the space (#) key.
        if(l == " "){
          l = "#";
          
          // find_letter finds the row and column for that character and updates current position to that key.
          count += find_letter(l);
          
          // if the letter is uppercase, check how many clicks it takes to first press the shift key then the main key.
        } else if(l == l.toUpperCase()) {
            if(!shift) {
              count += find_letter('*');
              shift = true;
              count += find_letter(l.toLowerCase());
            // if the letters are alread capitalized, continue finding the character.
          } else {
            count += find_letter(l.toLowerCase());
          }
          
          // if the keyboard is uppercase and the character we are looing to press is letter, press shift key then find the letter.
        } else if(shift && l == l.match(/[a-z]/i)){
            count += find_letter('*');
            shift = false;
            count += find_letter(l);
          
          // find the letter if keypad is lowercase.
        } else if(!shift && l == l.match(/[a-z]/i)){
          count += find_letter(l);
          
          // if the character isn't a letter or a space, find how many clicks to press the character.
        } else {
          count += find_letter(l);
        }
      }); 
          return count;
    };
    
    console.log(tvRemote("your"))
    
  • Custom User Avatar

    Thanks for the replies. I didn't realize that insert() wasn't as effecient as append.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    I'm still learning python. I took almost 50 filled lines of code to finally produce a winner. Your code looks clean, readable with a great use of functions.