How to return a string with capital letters?

0

I'm trying to make a function that by passing certain arguments (numbers that act as indexes), with those numbers will change the indexes of a string (which I transform into an array) to CAPITAL LETTERS, but it does not return anything to me ..

Second explanation:

A string I transformed into an array to work with its indexes and then the arguments sent, will be used to specify the indices and thus these indices of the array of the STRING, transform them to uppercase and then return them as a string with the join method, and with the capitals applied -

function f() { // Funcion
  
  var a = "hola"; // Variable
  var e = a.split(''); // Transformo en array
  var args = arguments.length; // Cantidad de argumentos
  var i = 0; // Contador para el for
  var finals = ""; // Variable para retornar string modificada
  
  var maximo = Math.max.apply(null, arguments); // el argumento mas alto
  
  for(;i<args;i++){ // recorro los argumentos
     
    
    if(arguments[i] > e.length -1) { // Si algun argumento es mayor que los elementos del string, retorna undefined
      
      finals = undefined; // undefined
      
    }
    else if(arguments[i] == e[i]) { // si el argumento recorrido es igual a algun indice de la string, por ejemplo ingresó como argumento 2 y cuando e[i] sea igual a e[2](mismo numero que el argumento) , la letra del string(que se transformó en array) sera modificada a MAYÚSCULA, y luego se le sumará el resto de la string con slice, desde el máximo de los argumentos(osea slice(comienzo,final))
      
      finals += e[i].toUpperCase() + e.slice(maximo,e.length);
      
    }
    
    
  }
  
   return finals;   // vuelvo a meter la string que habia sido convertida en array, a un string con join()
  
  
  
}

var a = f(2);

console.log(a);
    
asked by Eduardo Sebastian 09.07.2017 в 03:44
source

1 answer

1

Capitalizing the indicated ones

I do not know if I interpret what you need. But maybe this will help you move forward

function f() { // Funcion
  
  var a = "hola"; // Variable
  var e = a.split(''); // Transformo en array
  var args = arguments.length; // Cantidad de argumentos
  var finals = e; // Variable para retornar arranca como array "e"
  
  for(var i = 0;i<args;i++){ // recorro los argumentos
    var pos=arguments[i];
    if(pos>=0 && pos< e.length) {
      finals[pos] = e[pos].toUpperCase();
    }else{
      return undefined
    }
  }
  return finals.join('');
}

console.log('hoLa=',f(2));
console.log('hOlA=',f(1,3));
    
answered by 09.07.2017 / 04:13
source