Return Array in uppercase [duplicate]

-1

If I have:

        var e = "String";
        var f = e.split('');  
        var d = f;
        d += f[2].toUpperCase(); 

       /*   d + = f[2].toUpperCase().split(''); *///Segundo intento


    
    console.log(d.join(''));

How can I return the string as 'StRing' and not as 'StringR'?

and second, because the method join() , does not work correctly?

    
asked by Eduardo Sebastian 09.07.2017 в 04:28
source

2 answers

1

The code in the question has several problems.

On the one hand, a value is assigned to d, but then immediately another value is assigned. The second value assigned to d is a string of length one, and join returns error because it is a method of Array , not of String

Another problem is that the word string is used, since it is not advisable to use keywords or as variable values or as variable names because this leads to confusion.

In the following example, to avoid confusion instead of string as value, montaña is being used.

Note that only two variables are used, a and b .

var a = "montaña";
var b = a.split('');
b[2] = b[2].toUpperCase();
console.log(b.join(''));
    
answered by 09.07.2017 в 05:35
0

I answer in the code. Basically the problem is that if a is an Array a + 'x' is a string (the + first converts the array into string by entering commas in the middle).

    var e = "String"; 
    console.log('e',e,typeof e);
    var f = e.split('');  
    console.log('f',f,typeof f, f.constructor.name);
    var d = f;
    d += f[2].toUpperCase();
    // acá d se convirtió de nuevo en un string (por el +=) y luego se le agregó una letra al final en mayúsculas
    console.log('d',d,typeof d, d.constructor.name);
    
    // lo siguiente falla porque de es un String y los Strings no tienen join
    console.log(d.join(''));
    
answered by 09.07.2017 в 04:53