-1

I am very new with framer.js and I am just following a book called http://coffeescript-for-framerjs.com/#buy where it's written

fruits[1].toUpperCase() It was working properly

fruits.toUpperCase()

Here in book it's fruit not fruits i tried both but getting error

Now i got it that toUpperCase() is a string method so it will not work with Array. but what exactly book is saying here then.

edi9999
  • 19,701
  • 13
  • 88
  • 127
Jamna
  • 2,561
  • 7
  • 28
  • 22

5 Answers5

8

Why:

.toUpperCase() is a method of the String.prototype, not the Array.prototype.

An example of the correct syntax in your case:

var fruits = ['apple', 'orange', 'kiwi']

for (var i = 0; i < fruits.length; i++) {
    fruits[i] = fruits[i].toUpperCase();
}
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
b00t
  • 409
  • 3
  • 10
1

Fruits is an array. toUpperCase function is not defined on array but for string objects.

V31
  • 7,626
  • 3
  • 26
  • 44
1

Because fruits[1] referring to your array element which is of type String

toUpperCase() can only be used for String type.

fruits.toUpperCase() is failing fruits is of type Array

Read More here

Sunil B N
  • 4,159
  • 1
  • 31
  • 52
1

If you want to make it work that way:

Array.prototype.toUpperCase = function() {
    for(var i = 0; i < this.length; i++)
        if(typeof this[i] === 'string')
            this[i] = this[i].toUpperCase();
}

To test:

var fruits = ["oranges", "apples", "bananas"];
fruits.toUpperCase();
console.log(fruits.toString());

ORANGES,APPLES,BANANAS

Samurai
  • 3,724
  • 5
  • 27
  • 39
0

I was also able to solve it with the below:

let fruit = new Array("peach","orange", "mango", "apple" );
console.log(fruit.toString().toUpperCase());
mr NAE
  • 3,144
  • 1
  • 15
  • 35
  • 1
    @MakwanaPrahlad sure! So the other answers called the array to a string in one line and then called the string to all Uppercase in the next line. I opted to join them together in one line to show I was calling this array to a string to make it uppercase. I am new to coding so I don't know if condensing it this way is best practice, but it did work for me and saved an extra step – nabdevtech Dec 17 '20 at 19:57