Discuss Scratch

Pepperoni505
Scratcher
100+ posts

Is there JavaScript lists?

Hello. I was wondering that since you can Make variables in JavaScript Like:
var postExample = "Question";
I was wondering if you can make lists in JavaScript. Please tell me! Thanks.
WooHooBoy
Scratcher
1000+ posts

Is there JavaScript lists?

var array = ["item", "item2", "etc"]
Pepperoni505
Scratcher
100+ posts

Is there JavaScript lists?

WooHooBoy wrote:

var array = ["item", "item2", "etc"]
Thanks dude! And how do you get a specific item's info form the list?
Pepperoni505
Scratcher
100+ posts

Is there JavaScript lists?

WooHooBoy wrote:

var array = ["item", "item2", "etc"]
How do you name the list?
Firedrake969
Scratcher
1000+ posts

Is there JavaScript lists?

“array” is the name.
WooHooBoy
Scratcher
1000+ posts

Is there JavaScript lists?

Pepperoni505 wrote:

WooHooBoy wrote:

var array = ["item", "item2", "etc"]
Thanks dude! And how do you get a specific item's info form the list?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array


All of your needs.
ChocolatePi
Scratcher
1000+ posts

Is there JavaScript lists?

Here's a complete demo:
var list = [1, 2, 3] // Make a list and give it values 1 2 3
list[0] // item 1 of list (note that js arrays are 0 based - the first item is 0 instead of 1
list[0] = 'hi' // replace item 1 of list with 'hi'
list.splice(0, 0) // delete item 1 of list
list.length // length of list

You can look up more online!
CatsUnited
Scratcher
1000+ posts

Is there JavaScript lists?

Pepperoni505 wrote:

WooHooBoy wrote:

var array = ["item", "item2", "etc"]
Thanks dude! And how do you get a specific item's info form the list?
They're not called Lists though, they're called Arrays, and they're far more powerful then normal Scratch lists. You can add 2 dimensional lists, like
var array = ["item", ["item2", "item2.1"],"etc"]
ev3coolexit987654
Scratcher
1000+ posts

Is there JavaScript lists?

//example of array(2d)
var qwertyuiop = [["1,1","1,2"],["2,1","2,2"]];
var place11  = qwertyuiop[0][0];

Last edited by ev3coolexit987654 (Sept. 9, 2015 00:53:59)

Jonathan50
Scratcher
1000+ posts

Is there JavaScript lists?

CatsUnited wrote:

Pepperoni505 wrote:

WooHooBoy wrote:

var array = ["item", "item2", "etc"]
Thanks dude! And how do you get a specific item's info form the list?
They're not called Lists though, they're called Arrays, and they're far more powerful then normal Scratch lists. You can add 2 dimensional lists, like
var array = ["item", ["item2", "item2.1"],"etc"]
This is because, like Snap!, Arrays are first class.
Also unlike lists, arrays have a fixed amount of items.
You can't add or delete items, though items can be undefined or you can set an item that is undefined to another item.
If you initialize the Array like
var myArray = [, , , , , ,]; // an array with 7 items 
all items will be undefined.
Also notice that unlike Scratch, arrays are stored as values in normal variables, since they are first class.

Last edited by Jonathan50 (Sept. 7, 2015 01:48:18)

liam48D
Scratcher
1000+ posts

Is there JavaScript lists?

Jonathan50 wrote:

Also notice that unlike Scratch, arrays are stored as values in normal variables, since they are first class.

So with Scratch you'd do this to “make” your list:
delete (all v) of [your list v]
add [1] to [your list v]
add [2] to [your list v]
add [banana] to [your list v]
Direct translation to JavaScript:
var yourList;
yourList = [];
yourList.push(1);
yourList.push(2);
yourList.push("banana");

With JavaScript you'd do this to make your Array:
var yourList = [1, 2, "banana"];
Direct translation to Scratch(blocks, or rather, Snap!):
set [your list v] to (list [1] [2] [banana] :: list)
Pepperoni505
Scratcher
100+ posts

Is there JavaScript lists?

liam48D wrote:

Jonathan50 wrote:

Also notice that unlike Scratch, arrays are stored as values in normal variables, since they are first class.

So with Scratch you'd do this to “make” your list:
delete (all v) of [your list v]
add [1] to [your list v]
add [2] to [your list v]
add [banana] to [your list v]
Direct translation to JavaScript:
var yourList;
yourList = [];
yourList.push(1);
yourList.push(2);
yourList.push("banana");

With JavaScript you'd do this to make your Array:
var yourList = [1, 2, "banana"];
Direct translation to Scratch(blocks, or rather, Snap!):
set [your list v] to (list [1] [2] [banana] :: list)
What's JavaScript code for this block?
(item (3 v) of [myList  v])
powerpoint56
Scratcher
1000+ posts

Is there JavaScript lists?

Pepperoni505 wrote:

What's JavaScript code for this block?
(item (3 v) of [myList  v])
Bracket notation.
var myArray = ["apple", "pear", "banana"];
myArray[0] === "apple" && myArray[1] === "pear"
Pepperoni505
Scratcher
100+ posts

Is there JavaScript lists?

powerpoint56 wrote:

Pepperoni505 wrote:

What's JavaScript code for this block?
(item (3 v) of [myList  v])
Bracket notation.
var myArray = ["apple", "pear", "banana"];
myArray[0] === "apple" && myArray[1] === "pear"
But how does it know which item to get info out of?
liam48D
Scratcher
1000+ posts

Is there JavaScript lists?

Pepperoni505 wrote:

powerpoint56 wrote:

Pepperoni505 wrote:

What's JavaScript code for this block?
(item (3 v) of [myList  v])
Bracket notation.
var myArray = ["apple", "pear", "banana"];
myArray[[]0] === "apple" && myArray[1] === "pear"
But how does it know which item to get info out of?
It depends on the order you put then in your array. If you put “apple” into your array first, you can access it by using myArray, because it's the zeroth item in the array.

Like ChocolatePi said, arrays are zero-based - that means that the first item is arr[0], the second item is arr[1], the third item is arr[2], etc.

Super edit bump: Forgot to change the very first myArray[0]. rip

Last edited by liam48D (Oct. 1, 2015 08:33:02)

nathanprocks
Scratcher
1000+ posts

Is there JavaScript lists?

liam48D wrote:

Pepperoni505 wrote:

powerpoint56 wrote:

Pepperoni505 wrote:

What's JavaScript code for this block?
(item (3 v) of [myList  v])
Bracket notation.
var myArray = ["apple", "pear", "banana"];
myArray[0] === "apple" && myArray[1] === "pear"
But how does it know which item to get info out of?
It depends on the order you put then in your array. If you put “apple” into your array first, you can access it by using myArray[0], because it's the zeroth item in the array.

Like ChocolatePi said, arrays are zero-based - that means that the first item is arr[0], the second item is arr[1], the third item is arr[2], etc.
FTFY.
liam48D
Scratcher
1000+ posts

Is there JavaScript lists?

nathanprocks wrote:

liam48D wrote:

Pepperoni505 wrote:

powerpoint56 wrote:

Pepperoni505 wrote:

What's JavaScript code for this block?
(item (3 v) of [myList  v])
Bracket notation.
var myArray = ["apple", "pear", "banana"];
myArray[0] === "apple" && myArray[1] === "pear"
But how does it know which item to get info out of?
It depends on the order you put then in your array. If you put “apple” into your array first, you can access it by using myArray[0], because it's the zeroth item in the array.

Like ChocolatePi said, arrays are zero-based - that means that the first item is arr[0], the second item is arr[1], the third item is arr[2], etc.
FTFY.
Wat. oh…. I see xD I'll fix that

EDIT: It was the [1], [2], [3] not showing up he fixed

Last edited by liam48D (Sept. 7, 2015 14:14:33)

Smitop
Scratcher
100+ posts

Is there JavaScript lists?

Jonathan50 wrote:

Also unlike lists, arrays have a fixed amount of items.
But this code works…
var array = [1,2,3];
alert("array is" + array);
array[100] = 100;
alert(array);
Firedrake969
Scratcher
1000+ posts

Is there JavaScript lists?

Arrays are mutable.
Pepperoni505
Scratcher
100+ posts

Is there JavaScript lists?

Smitop wrote:

Jonathan50 wrote:

Also unlike lists, arrays have a fixed amount of items.
But this code works…
var array = [1,2,3];
alert("array is" + array);
array[100] = 100;
alert(array);
Thats True.

Powered by DjangoBB