0% found this document useful (0 votes)
2 views

java

The document covers various programming concepts including conditional statements, logical operators, loops, and functions in JavaScript. It explains the use of if-else statements, ternary operators, switch cases, and for loops, along with their syntax and examples. Additionally, it discusses assignment operators and provides a detailed example of a function to reverse a string.

Uploaded by

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

java

The document covers various programming concepts including conditional statements, logical operators, loops, and functions in JavaScript. It explains the use of if-else statements, ternary operators, switch cases, and for loops, along with their syntax and examples. Additionally, it discusses assignment operators and provides a detailed example of a function to reverse a string.

Uploaded by

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

const price = 15.

00;

const money = 20.00;

if (money >= price) {


console.log("buy the hammer");
} else {
console.log("don't buy the hammer");
}

const weather = "sunny";

if (weather === "snow") {


console.log("Bring a coat.");
} else if (weather === "rain") {
console.log("Bring a rain jacket.");
} else {
console.log("Wear what you have on.");
}

Logical operators
Logical operators can be used in conjunction with boolean values
(true and false) to create complex logical expressions.

By combining two boolean values together with a logical operator,


you create a logical expression that returns another boolean value.
Here’s a table describing the different logical operators:

Operat Meanin
Example How it works
or g
Logical value1 &&
&&
value2
Returns true if both value1 and value2 evaluate to true.
AND
value1 || Returns true if either value1 or value2 (or even both!)
|| Logical OR value2
evaluates to true.
Logical Returns the opposite of value1. If value1 is true, then !
! !value1
NOT value1 is false
TIP: Using if(isGoing) is the same as using if(isGoing === true) . Alternatively,
using if(!isGoing) is the same as using if(isGoing === false) .

Ternary operator
The ternary operator provides you with a shortcut alternative for
writing lengthy if...else statements.

conditional ? (if condition is true) : (if condition is false)


To use the ternary operator, first provide a conditional statement on
the left-side of the ?. Then, between the ? and : write the code that
would run if the condition is true and on the right-hand side of
the : write the code that would run if the condition is false. For
example, you can rewrite the example code above as:

const isGoing = true;


const color = isGoing ? "green" : "red";
console.log(color);
Prints: "green"
This code not only replaces the conditional, but it also handles the
variable assignment for color.

If you breakdown the code, the condition isGoing is placed on the left
side of the ?. Then, the first expression, after the ?, is what will be
run if the condition is true and the second expression after the, :, is
what will be run if the condition is false.
const option = 3;

switch (option) {
case 1:
console.log("You selected option 1.");
break;
case 2:
console.log("You selected option 2.");
break;
case 3:
console.log("You selected option 3.");
break;
case 4:
console.log("You selected option 4.");
break;
case 5:
console.log("You selected option 5.");
break;
case 6:
console.log("You selected option 6.");
break; // technically, not needed
}
Prints: You selected option 3.
const option = 23;

switch (option) {
case 1:
console.log("You selected option 1.");
break;
case 2:
console.log("You selected option 2.");
break;
case 3:
console.log("You selected option 3.");
break;
case 4:
console.log("You selected option 4.");
break;
case 5:
console.log("You selected option 5.");
break;
case 6:
console.log("You selected option 6.");
break;
default:
console.log("You did not select a valid option.");
}

or Loops Require a Start, Stop and Step


The for loop explicitly forces you to define the start point, stop point,
and each step of the loop. In fact, you'll get an Uncaught SyntaxError:
Unexpected token ) if you leave out any of the three required pieces.

for ( start; stop; step ) {


// do this thing
}
Here's an example of a for loop that prints out the values from 0 to
5. Notice the semicolons separating the different statements of the
for loop: var i = 0; i < 6; i = i + 1
for (let i = 0; i < 6; i = i + 1) {
console.log("Printing out i = " + i);
}
Prints:
Printing out i = 0
Printing out i = 1
Printing out i = 2
Printing out i = 3
Printing out i = 4
Printing out i = 5

Loops Repeat Blocks of Code


Conditional statements are one way to control the flow of code -- if a
certain condition is true, execute this block of code, otherwise,
execute that other block of code.

Loops are another way to control the flow of code by allowing us to


execute a block of code multiple times.

What We Will Cover in This Lesson

You will learn how to:

 Use while loops


 Use for loops
 Nest loops for more complex automation
 Use assignment operators to write more concise code

Along the way we'll give you a lot of practice writing loops.

Let's get started!

 Orbiter transfers from ground to internal power (T-50 seconds)

 Ground launch sequencer is go for auto sequence start (T-31


seconds)
 Activate launch pad sound suppression system (T-16 seconds)

 Activate main engine hydrogen burnoff system (T-10 seconds)

 Main engine start (T-6 seconds)

 Solid rocket booster ignition and liftoff! (T-0 seconds)

 for Loops Require a Start, Stop and Step


 The for loop explicitly forces you to define the start point, stop
point, and each step of the loop. In fact, you'll get an Uncaught
SyntaxError: Unexpected token ) if you leave out any of the three
required pieces.
 for ( start; stop; step ) {
 // do this thing
 }
 Here's an example of a for loop that prints out the values from
0 to 5. Notice the semicolons separating the different
statements of the for loop: var i = 0; i < 6; i = i + 1
 for (let i = 0; i < 6; i = i + 1) {
 console.log("Printing out i = " + i);
 }
 Prints:
Printing out i = 0
Printing out i = 1
Printing out i = 2
Printing out i = 3
Printing out i = 4
Printing out i = 5

Nested Loops Add Complexity


Nested loops are just loops inside of other loops. Take a look at
our demo code:

for (let x = 0; x < 3; x = x + 1) {


for (let y = 0; y < 2; y = y + 1) {
console.log(x + ", " + y);
}
}
Assignment Operators
An assignment operator is a shorthand way to peform a
mathematical operation on a variable and assigns that value to the
variable.

You can use assignment operators for addition, subtraction,


multiplication, and division.

// Add y to x
x += y // x = x + y

// Subtract y from x
x -= y // x = x - y

// Multiply x by x
x *= y // x = x* y

// Divide x by y
x /= y // x = x / y
These assignment operators will come in handy as you create more
loops!
What does this code log out? javascript let x = 4; x++; console.log(x);

*Enter only the numerical response -- no extra characters*

Functions Are Awesome!


The ability to generalize code for a variety of possible inputs is a
powerful tool when creating easy to understand, non-repetitive
code.

function reverseString(reverseMe) {
let reversed = "";
for (let i = reverseMe.length - 1; i >= 0; i--) {
reversed += reverseMe[i];
}
return reversed;
}
console.log(reverseString("Julia"));
Prints "ailuJ"
Let's break it down:

 The function has one parameter -- a variable named reverseMe.


 reverseMewill store the argument -- the value of the string that
we want the function to operate on.
 The variable reversed is intialized as an empty string. It will be
used to store the reversed string as as it is being constructed.
 The function loops through each character the reverseMe string
using string indexes, from the end to the beginning and adds
each character to reversed.
 When the loop is complete, reversed is returned.

Annotated Function

// Set one parameter to hold the value of the input string


function reverseString(reverseMe) {

// Declare a variable with an empty string to store the reversed


string
let reversed = "";

// Loop through the `reverseMe` string from back to front


for (let i = reverseMe.length - 1; i >= 0; i--) {

// Add each character to the end of `reversed`


reversed += reverseMe[i];
}
return reversed;
}

// Return the completed string when the loop is complete return


reversed; }
console.log(reverseString("Julia"));
Using a function simplifies the process and allows you to reuse the
function by calling it by its name and passing it in a string. If these
steps were not wrapped inside this reverseString function you would to
write all of this code each time you needed to reverse a string.
String Methods Review

Why does the loop start with reverseMe.length - 1 ?


Submit

const donuts = ["jelly donut", "chocolate donut", "glazed donut"];

donuts.forEach(function(donut) {
donut += " hole";
donut = donut.toUpperCase();
console.log(donut);
});

const newArray = myArray.map(function (elem) {


elem = elem + 100;
return elem;
});

console.log(newArray); //[101, 102, 103, 104, 105]

You might also like