Codehs Unit 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

1.

Karel is a dog who listens to your commands.


Karel Commands
- move () ;
- turnLeft () ;
- putBall () ;
- takeBall () ;

1.2

Horizontal – row – street


Vertical – column – avenue

1.3 & 1.4

A function is a way to teach Karel a new work. Functions allow us to break down our
program into smaller parts, and make the program easier to understand.
Defining a function: specifying the instructions for this new action
Calling a function: causing the action to actually happen

We use functions to:


- break down our program into smaller parts
- avoid repeated code
- make our program more readable

Rules for Defining a Function


- Name should start with a letter, and cannot have any spaces
- The name should describe what this function does
- The code in the function goes between the { and the } character, and this is called the
"function body."

3x turnLeft(); = turnRight();
2x turnLeft(); = turnAround();

1.5

Start function – all of our programs start by "calling" the start function.
function start () {
// Your code here
}

1.6

Top down design is a way of designing your program by starting with the biggest problem
and breaking it down into smaller and smaller pieces that are easier to solve.
The process of breaking a program down into smaller pieces is called decomposition, and
the component parts of a large problem are called subproblems.

1.7
Comments - a way for you to leave notes about what your code is doing in plain English, so
other people can understand it.

Commenting Code:
/* Leave a comment so a reader
*understands what your
*program is doing
*/
// This is a single line comment

Preconditions: What assumptions we make and what must be true before the function is
called.
Postconditions: What should be true after the function is called.

1.8
turnRight and turnAround are already defined in SuperKarel.

1.9
For Loop= Repeat Fixed Number of Times

for (var i = 0; i ‹ count; i++) {


/* code to execute count times */

Example:
/* This places ten balls down */
for (var i = 0; i < 10; i++){
putBall () ;
}

1.10
Crashing in Karel - When we have a mistake in our program, it is called a "bug." For
example, if Karel runs into a wall, that would be a bug.

We Can Ask Questions About Karel's World: Is Karel's front clear? Are there any balls in
Karel's current position? Is Karel facing east?

A condition is a function that returns a true/false answer.


Karel Conditions:
frontIsClear () frontIsBlocked ()
leftIsClear () leftIsBlocked ()
rightIsClear () rightIsBlocked ()
ballsPresent () noBallsPresent ()
facingNorth () facingSouth ()
facingEast () facingWest ()

If Statements
if (condition) {
/* code */
}

If Statement Example
if (frontIsClear ()){
move () ;
}

We use if statements in JavaScript to do something only if a condition is true

1.11
If/Else Statements

if (condition) {
* code if condition is true */
}else{
* code to run otherwise */

If/Else Example
if (frontIsClear ()) {
move () ;
}elsel{
turnLeft () ;
}

If statements and if/else statements let karel handle different types of worlds.
We went from solving very specific problems, and now can solve more general problems.

We use if/else statements in JavaScript to either do something if a condition is true or do


something else.

1.12
We Can Repeat Karel Actions – What if we want to move all the way to a wall?
While loops allow us to repeat a section of code as long as some condition is true.
While Loops = Repeat as Long as Condition is True

While Loop
while (condition){
// code to execute while
// condition is true

While Loop Example:
while (frontIsClear ()) {
move () ;
}

We use while loops in JavaScript to repeat some code while a condition is true

1.13
Control Structures
Ask Questions:
• If Statement
• If-Else Statement
Repeat Code
• For Loop
• While Loop

We use control structures to control the flow of the program; how the commands execute.

1.14

- You need to write a program that has Karel put down a tennis ball if the world has
dimensions of 1x1. Control structure - If Statement
- You need to write a program that has Karel move if the front is clear, but if it isn't clear,
Karel will do nothing. Control structure - If Statement
- You need to write a program where Karel will take a ball if there is a ball present,
otherwise Karel should put down a ball. Control structure - If/Else statement
- You need to write a program that has Karel move 6 times and then put a ball. Control
structure - For loop
- You need to write a program that has Karel take all the tennis balls where Karel is
standing if there are any there. Karel should end up with no tennis balls on that spot.
Control structure - While Loop
1.15
Why should a programmer indent their code?
- Helps show the structure of the code
- Easier for other people to understand
- Indenting is a key part of good programming style

1.16

1.17
Aditional examples:

 1.2.5 pyramid of Karel.png


 1.3.4 Slide Karel.png
 1.3.5: Fireman Karel.png
 1.4.4 Pancakes.png
 1.4.5 Mario Karel.png
 1.6.3 Hurdle Karel.png
 1.6.4 The Two Towers.png
 1.7.3 Hurdle Karel (coments).mov
 1.9.3 Repeated Move.png
 1.9.4 Put Down Tennis Balls.png
 1.9.7 For Loop Square.png
 1.9.8 Lots of Hurdles.png
 1.10.4 Safe Take Ball.png
 1.10.5 Is There a Ball?.png
 1.12.6 Big Tower.png

You might also like