You are on page 1of 11

JavaScript

Repeat command for x times:

for (var count = 0; count < 5; count++) {


moveForward();
}
Turn: turnLeft(); or turnRight();

Repeat until:

while (notFinished()) {
moveForward();
moveForward();
turnLeft();
}

If block:

while (notFinished()) {
moveForward();
if (isPathLeft())
{ turnLeft();
}
}
while (notFinished()) {
moveForward();
if (isPathRight()) {
turnRight();
}

If-else statement:

while (notFinished()) {
if (isPathForward()) {
moveForward();
} else {
turnLeft();
}
}

Repeat until:

while (notFinished()) {
if (isPathForward()) {
moveForward();
} else {
if (isPathRight()) {
turnRight();
} else {
turnLeft();
}
}
}

Drawing instructions:

moveForward(100);
turnRight(90);
moveForward(100);

Drawing a square:

penColour('#ff0000');

moveForward(100);
turnRight(90);
moveForward(100);
turnRight(90);
moveForward(100);
turnRight(90);
moveForward(100);
turnRight(90);

OR:
for (var count = 0; count < 4; count++) {
moveForward(100);
turnRight(90);
}

Drawing a triangle:

for (var count = 0; count < 3; count++) {


penColour(colour_random());
moveForward(100);
turnRight(120);
}

Drawing an envelope(triangle inside a square):

for (var count = 0; count < 3; count++) {


moveForward(100);
turnRight(120);
}
for (var count2 = 0; count2 < 4; count2++) {
moveForward(100);
turnRight(90);

Drawing a square above and near a triangle:

for (var count = 0; count < 3; count++) {


moveForward(100);
turnRight(120);
}
for (var count2 = 0; count2 < 4; count2++) {
moveBackward(100);
turnRight(90);
}

Drawing square glasses:

penColour('#228b22');
for (var count = 0; count < 4; count++) {
turnRight(90);
moveForward(100);
}
turnRight(90);
moveBackward(50);
for (var count2 = 0; count2 < 4; count2++) {
moveBackward(100);
turnLeft(90);
}

Drawing a circle:

for (var count = 0; count < 360; count++) {


penColour(colour_random());
penWidth(4);

moveForward(1);

Start ->

turnRight(1);
}

Drawing a triangle flower:

for (var count2 = 0; count2 < 4; count2++) {


penColour(colour_random());
for (var count = 0; count < 3; count++) {
moveForward(100);
turnRight(120);
}
turnRight(90);
}

Drawing a ladder with 10 steps:

for (var count2 = 0; count2 < 10; count2++) {


penColour(colour_random());
for (var count = 0; count < 4; count++) {
moveForward(20);
turnRight(90);
}
moveForward(20);
}

Drawing a square with 10 minisquares on each side:

for (var count3 = 0; count3 < 4; count3++) {


for (var count2 = 0; count2 < 10; count2++) {
penColour(colour_random());
for (var count = 0; count < 4; count++) {
moveForward(20);
turnRight(90);
}

moveForward(20);
}
turnRight(90);
}

While statement:

while (pilePresent()) {
dig();
moveForward();
}

When you dont know how many times to repeat:

turnRight();
moveForward();
while (holePresent()) {
fill();
}

Fill in a hole at the end of the road:

while (isPathForward()) {
moveForward();
}
fill();

If there are obstacles remove them:

while (isPathForward()) {
moveForward();
if (pilePresent()) {
dig();
}
}

Functions: Drawing a square

// draw_a_square
for (var count = 0; count < 4; count++) {
moveForward((100));
turnRight(90);
}

Drawing several different squares using a counter:

var counter;
for (counter = 50; counter <= 90; counter += 10) {
// draw_a_square
for (var count = 0; count < 4; count++) {
moveForward((counter));
turnRight(90);
}
}

Drawing a spirale:

var counter;
for (counter = 25; counter <= 60; counter += 5) {
moveForward(counter);
turnRight(90);
}

Define a function:

function fill_5() {
for (var count = 0; count < 5; count++) {
fill();
}
}
moveForward();
fill_5();

Drawing a square calling a function:

function draw_a_square() {
for (var count = 0; count < 4; count++) {
moveForward(100);
turnRight(90);
}
}

function draw_a_circle() {
for (var count2 = 0; count2 < 360; count2++) {
moveForward(1);
turnRight(1);
}
}

draw_a_square();

JAVAScript Codecademy
//Comment lines
Confirm (I am ready); //Afiseaza o caseta de confirm cu mesajul introdus;
returneaza val TRUE

Var age;

//Declar var age unde vreau sa stochez varsta;

age=prompt("What's your age");


if(age<13)
fara griji

//Caseta de dialog unde introduce varsta

//Daca age<13 atunci afis ca joaca pe respo sa altfel poate juca

{
console.log("You're allowed to play under your legal");
}
else
{
console.log("Let's role!");
}

Semnul + concateneaza doua string-uri.

To call a function: function_name(input value);

Sintaxa unei functii:


var greeting = function (name) {
console.log("Great to see you," + " " + name);
};
greeting("Cedric");

//call the function

Functie care returneaza rezultatul cumpararii a 5 portocale insumate

var orangeCost=function(orangeCost){
var price=orangeCost*5;
console.log(price);
};
orangeCost(5);

Definirea unei functii cu 2 variabile care calculeaza perimetrul.

var perimeterBox = function(length,width){


return length+length+width+width;
}
perimeterBox(3,4);

Make the variable equal to the answer we get by asking the user "Do
you choose rock, paper or scissors?"

var userChoice = prompt("Do you choose rock, paper or scissors?");


var computerChoice = Math.random(); //If we declare a variable and make it equal
toMath.random(), that variable will equal a number between 0 and 1.

The loop statement:

for (var i = 1; i < 11; i = i + 1){


console.log(i);
}//afiseaza numerele de la 1 la 10.

var names = ["Ion", "Maria", "Stefan", "Flacau", "Mary"]; //afiseaza fiecare


nume din array
for(i=0; i<names.length; i++){
console.log("I know someone called", names[i]);}

Declare an empty array: var hits = [];


var text = "Hello, Cedric! There is Cedric, next to Cedric"; //declare a string
var myname = "Cedric";
Declare a string:
var myname = "Cedric";
for(i=0;i<name.length;i++){
if(text[i]==="C"){}
}//whether the current letter is equal to the first letter of your name.
(Capitalization counts!)

Finally, in the body of your loop, have your program use the .push() method of hits. Just
like strings and arrays have a .length method, arrays have a .push() method that adds the
thing between parentheses to the end of the array. For example,
newArray = [];
newArray.push('hello');

newArray[0]; // equals 'hello'

You might also like