virtual keyboard - version 1

by samur3

JavaScript

let keyboard = "a,b,c,d,e,f,g,h,i,j,k,l";
let w = 5;
let input = "e,l";

function getKeyboardPath(keyboard,w,input){
	let path = ''
  keyboard = keyboard.split(',');
  const keyboardLength = keyboard.length;
  input = input.split(',');
  
	//let arr = buildArrayFromInput(w,keyboard);
  debugger;
  let map = buildMapInputCordination(input,w,keyboardLength);
  
  return buildPath(input,map);    
}

function buildPath(input,map){
	let path = '';  
	path = getPath({x:0,y:0},map[input[0]],path);
  for(let i=1; i < input.length; i++){
  	path = getPath(map[input[i-1]],map[input[i]],path);
  }
  return path;
}

function getPath(from,to,path){
	if(from.x === to.x && from.y === to.y) return '!';
  while(from.x !== to.x || from.y !== to.y){
  	if(from.x > to.x) {
    	path += getDirectionValue('x-');
      from.x--;
    }
    else if(from.x < to.x) {
    	path += getDirectionValue('x+');;
      from.x++;
    }
    else if(from.y < to.y) {
    	path += getDirectionValue('y+');
      from.y++;
    }
    else if(from.y > to.y) {
    	path += getDirectionValue('y-');
      from.y--;
    }
  }
  path += '!';
  return path;
}

function getDirectionValue(value){
	switch(value){
  	case 'x+':
    return 'R';
    case 'x-':
    return 'L';
    case 'y+':
    return 'D';
    case 'y-':
    return 'U';
  }
}

function buildArrayFromInput(wide,keyboard){	
	let result = [];  
  
  let counter = 0
  let arr = [];
  for(let i=0; i < keyboard.length; i++){
  	arr.push(keyboard[i]);
    counter++;
  	if(counter === wide){
    	result.push(arr);
      arr = [];
      counter = 0;
    }
  }
  if(arr.length > 0) result.push(arr);
  return result;
}

/*function buildMapInputCordination(arr,input){
	let map = {};  
  input.forEach(char => {
  	for(let row=0; row < arr.length; row++){
    	let index= arr[row].indexOf(char);
      if(index > -1){
        map[char] = {
          x: index,
          y: row
        };
        break;
      }
    }
  });
  
  return map;
}*/

function...