This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#151 2012-10-16 08:39:37

mythbusteranimator
Scratcher
Registered: 2012-02-28
Posts: 1000+

Re: Javascript topic

fanofcena wrote:

transparent wrote:

trinary wrote:


It can be occasionally useful.

Well, I never use it because of inspect, but I always thought it was cool when I was first learning it.  smile

I have used the contenteditable attribute though to create custom facebook like inputs :-) its cool.. allows u to do things that a normal textbox or input cant [ rich input  big_smile  ]

Inspect element you can color, bold, add images, etc.


http://www.foxtrot.com/comics/2012-04-01-fdb37077.gif
clicky

Offline

 

#152 2012-10-17 09:17:22

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: Javascript topic

mythbusteranimator wrote:

fanofcena wrote:

transparent wrote:


Well, I never use it because of inspect, but I always thought it was cool when I was first learning it.  smile

I have used the contenteditable attribute though to create custom facebook like inputs :-) its cool.. allows u to do things that a normal textbox or input cant [ rich input  big_smile  ]

Inspect element you can color, bold, add images, etc.

*facepalm*

Do you expect the user to be able to use inspector ? seriously ?


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#153 2012-10-20 07:45:04

dvd4
Scratcher
Registered: 2010-06-30
Posts: 1000+

Re: Javascript topic

fanofcena wrote:

mythbusteranimator wrote:

fanofcena wrote:


I have used the contenteditable attribute though to create custom facebook like inputs :-) its cool.. allows u to do things that a normal textbox or input cant [ rich input  big_smile  ]

Inspect element you can color, bold, add images, etc.

*facepalm*

Do you expect the user to be able to use inspector ? seriously ?

it makes sense using inspector


I made a mod  big_smile  It's called blook!
http://i49.tinypic.com/16ia63p.png

Offline

 

#154 2012-11-23 23:23:27

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: Javascript topic

I need help.
I'm making a tic tac toe game, and I want to check if X or O won.
I have a variable called "grid", that is a multidimensional array.
In the middle of a game, it would look sorta like this (if I were declare it as if it were in the middle of a game)

Code:

grid[0]=[0,0,1]; //1's are X's, and 2's are O's
grid[1]=[1,2,2];
grid[2]=[1,2,2];

To look at it as if it were on the screen, you would have to flip it 90 degrees, to make it so I can do grid[x][y], and not grid[y][x], but that's not the point.

In the function in which I check if someone one, I don't know how to check grid[0], or any in that matter. It doesn't work, I had it log this:

Code:

console.log(grid[0]); //this returned 0,0,0
console.log(grid[0]==0,0,0); //this returned false
console.log(grid[0]==[0,0,0]); //this returned false

I also tried it with the 3 equal sign condition, but it still didn't return true (grid[0] was 0,0,0 according to the console log).

What did I do wrong?

EDIT: Just checked

Code:

console.log(grid[0]==000)//also with the 3 equal condition

and still didn't work...

Last edited by TorbyFork234 (2012-11-23 23:37:05)

Offline

 

#155 2012-11-24 06:27:49

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Javascript topic

In JS, you can't equality-test arrays (something I truly hate).

Code:

console.log(grid[0][0]==0 && grid[0][1]==0 && grid[0][2]==0);

Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#156 2012-11-25 17:22:59

elfin8er
Scratcher
Registered: 2012-01-15
Posts: 1000+

Re: Javascript topic

I've been wanting too really badly. I may actually do that now.


http://i1340.photobucket.com/albums/o723/stevetheipad/goldlittlesig.png

Offline

 

#157 2012-11-29 06:08:23

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: Javascript topic

Hardmath123 wrote:

In JS, you can't equality-test arrays (something I truly hate).

Code:

console.log(grid[0][0]==0 && grid[0][1]==0 && grid[0][2]==0);

Wait whaaaaaaaaaaa ?


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#158 2012-11-29 08:23:19

transparent
Scratcher
Registered: 2011-04-19
Posts: 1000+

Re: Javascript topic

It's a boolean statement to see if the following was true or not.


yes, yes i do.

Offline

 

#159 2012-12-02 19:31:50

scratchisthebest
Scratcher
Registered: 2009-02-08
Posts: 500+

Re: Javascript topic

&& is boolean and.
So raining && cold means "raining and cold".
Or one && two && three means "one and two and three".
Or equals1 && equals2 && equals3... or grid[0][0]==0 && grid[0][1]==0 && grid[0][2]==0


bye 1.4, we all loved you. but we all outgrew the site. 2.0 is a welcome change.
http://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.png

Offline

 

#160 2012-12-02 19:44:42

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: Javascript topic

scratchisthebest wrote:

&& is boolean and.
So raining && cold means "raining and cold".
Or one && two && three means "one and two and three".
Or equals1 && equals2 && equals3... or grid[0][0]==0 && grid[0][1]==0 && grid[0][2]==0

I know that, and that's what I did (since it's been about a week since I posted that).
I just wanted to know that why I can't do

Code:

grid[0]==[0,0,0]

?
I copy and pasted what it reported to an alert box (and also the console), and put that in the if statement. I kept on altering to see what kind it worked, but it didn't work. I was just wondering why it doesn't work. Doesn't the variable report what it contains, since it's apparently not doing that.

Offline

 

#161 2012-12-03 08:38:40

mythbusteranimator
Scratcher
Registered: 2012-02-28
Posts: 1000+

Re: Javascript topic

Say

Are layers like Sprites?

(Just curious)


http://www.foxtrot.com/comics/2012-04-01-fdb37077.gif
clicky

Offline

 

#162 2012-12-03 18:41:45

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: Javascript topic

mythbusteranimator wrote:

Say

Are layers like Sprites?

(Just curious)

Well, kind of, if you're talking about Scratch layers -> javascript sprites, then not really.
Scratch layers are like the z-index of CSS, in which you can manipulate with javascript (if you know how).
If you're talking about Javascript sprites. Then, no.
If you want to set up a sprite (like how it is in Scratch), in javascript, you would have to make an object, and assign it all the attributes that scratch has, and also give it it's own images (or sprite strip images).

Offline

 

#163 2012-12-04 15:31:15

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: Javascript topic

i have a question, how do you check if a string is valid hex code

function isValidHexCode(hexCode){
    if(hexCode.length!==6){ //I know you can get three-character hex codes that are valid but I'll do that later
        return FALSE;
    }
    var i;
    var allowed=new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f");
    for(i=0; i<6; i++){ //Check all characters separately, so check the first character, then the second, etc.
        if(!hexCode.charAt(i).toLowerCase() in allowed){
            return FALSE;
        }
    }
    return TRUE;
}

i tried that but it's not working

i tested it with

if(isValidHexCode("aaaaaa")){
    alert("Yes");
}else{
    alert("No");
}

if(isValidHexCode("zzzzzz")){
    alert("Yes");
}else{
    alert("No");
}

but it just wasn't alerting anything
i think it's the "in" part on line 8, i've never heard about the "in" operator before

i've probably made dozens of syntax errors i don't really know js too well


Why

Offline

 

#164 2012-12-04 16:41:24

mythbusteranimator
Scratcher
Registered: 2012-02-28
Posts: 1000+

Re: Javascript topic

Do you need an "else" statement?


http://www.foxtrot.com/comics/2012-04-01-fdb37077.gif
clicky

Offline

 

#165 2012-12-04 17:42:55

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: Javascript topic

Where


Why

Offline

 

#166 2012-12-04 18:09:29

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: Javascript topic

Code:

function isValidHexCode(hexCode){
    if(hexCode.length!==6){ //I know you can get three-character hex codes that are valid but I'll do that later
        return 0;
    }
    var i;
    var allowed=new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f");
    for(i=0; i<6; i++){ //Check all characters separately, so check the first character, then the second, etc.
        if(allowed.indexOf(hexCode.charAt(i).toLowerCase()) == -1){
            return 0;
        }
    }
    return 1;
}

I support the Free Software Foundation. Protect our digital rights!

Offline

 

#167 2012-12-04 19:23:44

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: Javascript topic

RedRocker227 wrote:

i have a question, how do you check if a string is valid hex code

function isValidHexCode(hexCode){
    if(hexCode.length!==6){ //I know you can get three-character hex codes that are valid but I'll do that later
        return FALSE;
    }
    var i;
    var allowed=new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f");
    for(i=0; i<6; i++){ //Check all characters separately, so check the first character, then the second, etc.
        if(!hexCode.charAt(i).toLowerCase() in allowed){
            return FALSE;
        }
    }
    return TRUE;
}

i tried that but it's not working

i tested it with

if(isValidHexCode("aaaaaa")){
    alert("Yes");
}else{
    alert("No");
}

if(isValidHexCode("zzzzzz")){
    alert("Yes");
}else{
    alert("No");
}

but it just wasn't alerting anything
i think it's the "in" part on line 8, i've never heard about the "in" operator before

i've probably made dozens of syntax errors i don't really know js too well

from what I saw in w3schools (I'm no javascript expert, I still use it), there is no "in" operator. The only time that 'in' is used as something inside a parameter (besides for user defined functions), is the for (x in object).
What you should do instead is using IndexOf().
Although it is used for finding the item #, you can replicate the

<[list v] contains [thing v]>
by having this

Code:

console.log(!arrayObject.indexOf('thing')==-1);

that should do what you want in the code above, if it's still wrong, then I don't know.

Edit: didn't scroll through bobbybee's code. It's shown in there too.
Sorry bobby.

Last edited by TorbyFork234 (2012-12-04 19:24:24)

Offline

 

#168 2012-12-04 20:19:09

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: Javascript topic

The "in" is from coffeescript.


I support the Free Software Foundation. Protect our digital rights!

Offline

 

#169 2012-12-05 02:53:39

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: Javascript topic

oh okay

thanks


Why

Offline

 

#170 2012-12-05 02:56:28

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: Javascript topic

mythbusteranimator wrote:

Do you need an "else" statement?

you don't need one, since the only way it will get to the part where it returns TRUE is if the hex code is valid
if it's not valid, it will never reach it to that point

also it's interesting that you have to return 0 or 1 instead of FALSE and TRUE
i guess i should research these things before i assume it's the same as in php


Why

Offline

 

#171 2012-12-05 17:18:25

TheSupremeOverLord
Scratcher
Registered: 2012-09-29
Posts: 100+

Re: Javascript topic

Java scripts great!  I am making a chatbot.  Also an RPGs, platformer, matrices calculator, and a few others.  It's super cool.  Also I use processing.js.  I am making a mod where it converts scratch into JavaScript.  Overall, great language!  My mom hates bit when I commentate in JavaScript.


http://i1154.photobucket.com/albums/p522/lizzyhipo/MINIPIG.jpg

Offline

 

#172 2012-12-05 18:00:35

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: Javascript topic

RedRocker227 wrote:

mythbusteranimator wrote:

Do you need an "else" statement?

you don't need one, since the only way it will get to the part where it returns TRUE is if the hex code is valid
if it's not valid, it will never reach it to that point

also it's interesting that you have to return 0 or 1 instead of FALSE and TRUE
i guess i should research these things before i assume it's the same as in php

I didn't actually know either, but it gave my syntax errors (and that kept it happy).


I support the Free Software Foundation. Protect our digital rights!

Offline

 

#173 2012-12-05 18:50:31

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: Javascript topic

RedRocker227 wrote:

mythbusteranimator wrote:

Do you need an "else" statement?

you don't need one, since the only way it will get to the part where it returns TRUE is if the hex code is valid
if it's not valid, it will never reach it to that point

also it's interesting that you have to return 0 or 1 instead of FALSE and TRUE
i guess i should research these things before i assume it's the same as in php

you don't.
it works fine when you do

Code:

return true;

I never seemed to be able to do this, so can someone help me: I don't know how to turn a hex code into RGB,be able to edit the values, and then turn it back into hex.
Basically replicating the

change pen color by (number)

Last edited by TorbyFork234 (2012-12-05 18:50:43)

Offline

 

#174 2012-12-05 19:17:12

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: Javascript topic

Hex code to RGB:

Seperate it into letters:

0xFF0000=

red = FF

green = 00

blue = 00;

Convert each value from hexadecimal to decimal (google it).

Modify values.

red = 255
green=255
blue=0

Convert each value from decimal to hexadecimal.

Concatenate together in order red, green, then blue.

0xFFFF00.

??

??

Profit!


I support the Free Software Foundation. Protect our digital rights!

Offline

 

#175 2012-12-05 19:19:13

technoguyx
Scratcher
Registered: 2008-10-18
Posts: 1000+

Re: Javascript topic

You'd just have to convert the hex values of the RGB color to decimal - this gives you an integer between 0 (0x00) and 255 (0xFF) for each color.

edit: dhrnkljsgrnjklsgj ninja'd. I was going to explain how to convert from hex to decimal, but it wasn't clear :c Have a link instead.

Last edited by technoguyx (2012-12-05 19:21:27)


http://getgnulinux.org/links/en/linuxliberated_4_78x116.png

Offline

 

Board footer