Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
22 views

JavaScript Function

Uploaded by

antei kuwute
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

JavaScript Function

Uploaded by

antei kuwute
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

JavaScript Function

/* FUNCTION is allowing you to create a series of an instruction, package it into a block of code, and
then you can use

the package to make it execute the series of code that you've writed between the curly brace.*/

//now i'll make a function named getMilk with alert.

/* function getMilk() { //start form this curly brace the instruction will be writed

alert("leaveHouse");

alert("moveRight");

alert("moveRight");

alert("moveUp");

alert("moveUp");

alert("moveUp");

alert("moveUp");

alert("moveRight");

alert("moveRight");// all of the alert between the curly braces is the insturction. the istruction can
be anything, console.log or any other script you will write.

alert("buyMilk");

alert("moveLeft");

alert("moveLeft");

alert("moveDown");

alert("moveDown");

alert("moveDown");

alert("moveDown");

alert("moveLeft");

alert("moveLeft");

alert("enterHouse");

} */ //and this closed curly brace is meaning that the istruction is done.

//to execute the function you do it like this:

/* getMilk(); */ //this will give you the alert because the script is using
//or if you want to use console.log this is the example:

function buyMilk(){

console.log("leaveHouse");

console.log("moveRight");

console.log("moveRight");

console.log("moveUp");

console.log("moveUp");

console.log("moveUp");

console.log("moveUp");

console.log("moveRight");

console.log("moveRight");

console.log("buyMilk");

console.log("moveLeft");

console.log("moveLeft");

console.log("moveDown");

console.log("moveDown");

console.log("moveDown");

console.log("moveDown");

console.log("moveLeft");

console.log("moveLeft");

console.log("enterHouse");

buyMilk(); // just like that.

/*So here, we are using stanford karel bot that we can give instruction with the function.

this is the code we are using to make the karel bot go to top right corner from bottom left corner on
5x5 world:

function main(){

topRightCorner();
}

function topRightCorner(){

moveTwoTimes();

turnLeft();

moveFourTimes();

turnRight();

moveTwoTimes();

function moveTwoTimes(){

move();

move();

function moveFourTimes(){

move();

move();

move();

move();

And this is another excercise, we give an instruction to make the bot placing a square from bottom
left corner

to the top right corner and ended up making a diagonal line of the 5x5 world:

function main(){

putBeeper();

puttingBeeper();

puttingBeeper();

puttingBeeper();

puttingBeeper();
}

function puttingBeeper(){

move();

turnLeft();

move();

putBeeper();

turnRight();

Now this is the excercise tocreate a chessboard pattern:

function main(){

putBeeper();

putBeeperMoveTwoTimes();

putBeeperMoveTwoTimes();

putBeeperMoveUpLeft();

putBeeperMoveTwoTimes();

putBeeperMoveUpRight();

putBeeperMoveTwoTimes();

putBeeperMoveTwoTimes();

putBeeperMoveUpLeft();

putBeeperMoveTwoTimes();

putBeeperMoveUpRight();

putBeeperMoveTwoTimes();

putBeeperMoveTwoTimes();

function putBeeperMoveTwoTimes(){

move();

move();
putBeeper();

function putBeeperMoveUpLeft(){

turnLeft();

move();

turnLeft();

move();

putBeeper();

function putBeeperMoveUpRight(){

move();

turnRight();

move();

putBeeper();

turnRight();

*/

/* PARAMETERS and ARGUMENTS, now we'll set the value on the parentheses that's empty on the
function before (example: getMilk()).

now this is the explanation*/

function getSoda(bottles){ //so we'll use the empty parentheses to make a value and utilise the value
on the parentheses. And we're creating the function this time.

var cost = bottles * 2;

console.log(cost) //the bottles on the var, will have a value after the function is called, when we
call the function, we must set the value of bottles in the parentheses.

//Example:
getSoda(2); //now we are calling the function with the bottles settled by 2. and the result on the
console will be 4.

//Excercise:

function buySoda(money){

var cost = money / 1.5;

console.log("leaveHouse");

console.log("moveRight");

console.log("moveRight");

console.log("moveUp");

console.log("moveUp");

console.log("moveUp");

console.log("moveUp");

console.log("moveRight");

console.log("moveRight");

console.log("your robot is only have $" + money + " so he will buy only " + Math.floor(cost) + "
soda." );

console.log("moveLeft");

console.log("moveLeft");

console.log("moveDown");

console.log("moveDown");

console.log("moveDown");

console.log("moveDown");

console.log("moveLeft");

console.log("moveLeft");

console.log("enterHouse");

buySoda(20);
/* OUTPUTS & RETURN VALUES, we will try to get a return from a function and calling it as a value
from a variable*/

function getCoffee (money, perCoffeeCost){

var coffeeCost = money / perCoffeeCost;

console.log("You bought " + Math.floor(coffeeCost) + " and you get " + money%perCoffeeCost + "
for the change.");

return money % perCoffeeCost; /* this return can be anything if you want multiply, or divide, or just
a variable */ /*so the return keyword is used for making output of the function. (result keyword is
used within a function to specify the value that the function should return

when it is called or invoked When a function is executed and the return statement is encountered,
the function stops its execution and returns the specified value to the calling code.)

based on this, the next code that are writed will not be readed because the executing process is
stopped. */

//Example, the code will be not noticed or readed:

var twoCoffeeCost = perCoffeeCost * 2;

console.log(twoCoffeeCost); //and the code is not going to be showed on the console.

var change = getCoffee(30,1.5); /*and when you call the function on variable as a value, it has a value
and not being undefined.

also you can use the function itself based on the return that you've writed. You can use the return on
another function by calling the function itself, so makesure

that you have writed the function instruction correctly and return the right value from the function
you called.*/

console.log(change);

//Example with using return on another function:

function coffeeCalc(money, oneCoffeeCost){ //this is function for buying coffee

var calculate = money * oneCoffeeCost;

return calculate;

function coffeeChange(money, oneCoffeeCost){ //and this is function for teh change


var calculate = money % oneCoffeeCost;

return calculate;

function buyCoffee(money){

console.log("leaveHouse");

console.log("moveRight");

console.log("moveRight");

console.log("moveUp");

console.log("moveUp");

console.log("moveUp");

console.log("moveUp");

console.log("moveRight");

console.log("moveRight");

console.log("your robot is only have $" + money + " so he will buy only " +
Math.floor(coffeeCalc(money, 1.5)) + " coffee, " + "and you get " + coffeeChange(money, 1.5) + " as a
change. "); //just call the function and the input, the return is based on the instruction you've writed
on the function.

console.log("moveLeft");

console.log("moveLeft");

console.log("moveDown");

console.log("moveDown");

console.log("moveDown");

console.log("moveDown");

console.log("moveLeft");

console.log("moveLeft");

console.log("enterHouse");

buyCoffee(35);

You might also like