Skip to content

Commit d500c23

Browse files
authored
Add files via upload
1 parent 5d7bf3a commit d500c23

File tree

1 file changed

+372
-0
lines changed

1 file changed

+372
-0
lines changed

javascript-conditional-examples.js

Lines changed: 372 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,372 @@
1+
// Logical operators can be used in conjunction with boolean values (true and false) to create complex logical expressions.
2+
// By combining two boolean values together with a logical operator,
3+
// you create a logical expression that returns another boolean value.
4+
// Here’s a description of the different logical operators:
5+
// && Logical AND value1 && value2 Returns true if both value1 and value2 evaluate to true.
6+
// || Logical OR value1 || value2 Returns true if either value1 or value2 (or even both!) evaluates to true.
7+
// ! Logical NOT !value1 Returns the opposite of value1. If value1 is true, then !value1 is false.
8+
9+
10+
!(4 === 4) && "STRing" === "STRing"
11+
// Returns: false
12+
13+
//Example of an if statement using a logical operator
14+
var colt = "not busy";
15+
var weather = "nice";
16+
17+
if (colt === "not busy" && weather === "nice") {
18+
console.log("go to the park");
19+
}
20+
21+
//Example of an else if statement using a logical operator
22+
23+
var dayOfTheWeeek = "Monday";
24+
var weather = "cloudy";
25+
26+
if (weather === "cloudy" || dayOfTheWeeek === "Tuesday") {
27+
console.log("Fey don't have to go to work");
28+
} else if (weather === "cloudy") {
29+
console.log("No Work");
30+
} else if (dayOfTheWeeek === "Monday") {
31+
console.log("Work");
32+
} else {
33+
console.log("Work, Work, Work");
34+
}
35+
36+
37+
// change the values of `balance`, `checkBalance`, and `isActive` to test your code
38+
var balance = 0;
39+
var checkBalance = true;
40+
var isActive = true;
41+
42+
// your code goes here
43+
//currently not finished yet!
44+
if (checkBalance === false) {
45+
console.log("Thank you. Have a nice day!");
46+
} else if (checkBalance === true || isActive === false ) {
47+
console.log("Your acconut is no longer active");
48+
//} else if (checkBalance === true && balance > 0 ) {
49+
// console.log("Your balance is $" + balance + ".");
50+
} else if (checkBalance === true && isActive === true && balance === 0 ) {
51+
console.log("Your acconut is empty.");
52+
} else if (checkBalance === true && isActive === true && balance < 0 ) {
53+
console.log("Your balance is negative. please contact bank.");
54+
} else {
55+
console.log("Your balance is $" + balance + ".");
56+
}
57+
58+
// or
59+
if (checkBalance === false) {
60+
console.log("Thank you. Have a nice day!");
61+
} else if (checkBalance === true || isActive === true && balance === 0 ) {
62+
console.log("Your acconut is empty.");
63+
} else if (checkBalance === true || isActive === false ) {
64+
console.log("Your acconut is no longer active");
65+
//} else if (checkBalance === true && balance > 0 ) {
66+
// console.log("Your balance is $" + balance + ".");
67+
} else if (checkBalance === true || isActive === true && balance < 0 ) {
68+
console.log("Your balance is negative. please contact bank.");
69+
} else {
70+
console.log("Your balance is $" + balance + ".");
71+
}
72+
73+
74+
75+
// change the values of `flavor`, `vessel`, and `toppings` to test your code
76+
var flavor = "vanilla";
77+
var vessel = "cone";
78+
var toppings = "cookies";
79+
80+
// Add your code here
81+
82+
if (flavor === ("vanilla" || "chocolate") || vessel === ("cone" || "bowl") || toppings === ("sprinkles" || "peanuts") ) {
83+
console.log("I'd like two scoops of " + flavor + " ice cream in a " + vessel + " with " + toppings + ".");
84+
} else {
85+
console.log("No Ice cream");
86+
}
87+
88+
89+
// change the values of `shirtWidth`, `shirtLength`, and `shirtSleeve` to test your code
90+
//currently not finished yet!
91+
var shirtWidth = 19;
92+
var shirtLength = 28;
93+
var shirtSleeve = 8.39;
94+
95+
// your code goes here
96+
if ((shirtWidth >= 18 || shirtWidth < 20) && shirtLength === 28 && shirtSleeve <= 8.38 ) {
97+
console.log("small (S)");
98+
} else if (shirtWidth < 20 && shirtLength === 29 && shirtSleeve < 8.38 ) {
99+
console.log("medium (M)");
100+
} else if (shirtWidth < 22 && shirtLength === 30 && shirtSleeve < 8.63 ) {
101+
console.log("large (L)");
102+
} else if (shirtWidth < 24 && shirtLength === 31 && shirtSleeve < 8.88 ) {
103+
console.log("extra large (XL)");
104+
} else if (shirtWidth < 26 && shirtLength === 33 && shirtSleeve < 9.63 ) {
105+
console.log("extra extra large (2XL)");
106+
} else if (shirtWidth < 28 && shirtLength === 34 && shirtSleeve < 10.13 ) {
107+
console.log("extra extra extra large (3XL)");
108+
} else {
109+
console.log("N/A");
110+
}
111+
112+
// or
113+
var shirtWidth = 19;
114+
var shirtLength = 28;
115+
var shirtSleeve = 8.39;
116+
117+
// your code goes here
118+
if ((shirtWidth >= 18 || shirtWidth < 20) && shirtLength === 28 && shirtSleeve <= 8.38 ) {
119+
console.log("small (S)");
120+
} else if ((shirtWidth >= 20 || shirtWidth < 22) && shirtLength === 29 && shirtSleeve < 8.63 ) {
121+
console.log("medium (M)");
122+
} else if ((shirtWidth >= 22 || shirtWidth < 24) && shirtLength === 30 && shirtSleeve < 8.88 ) {
123+
console.log("large (L)");
124+
} else if ((shirtWidth >= 24 || shirtWidth < 26) && shirtLength === 31 && shirtSleeve < 9.63 ) {
125+
console.log("extra large (XL)");
126+
} else if ((shirtWidth >= 26 || shirtWidth < 28) && shirtLength === 33 && shirtSleeve < 10.13 ) {
127+
console.log("extra extra large (2XL)");
128+
} else if ((shirtWidth >= 28 || shirtWidth < 30) && shirtLength === 34 && shirtSleeve === 10.13 ) {
129+
console.log("extra extra extra large (3XL)");
130+
} else {
131+
console.log("N/A");
132+
}
133+
134+
135+
//A value is falsy if it converts to false when evaluated in a boolean context.
136+
//For example, an empty String "" is falsy because, "" evaluates to false
137+
if ("") {
138+
console.log("the value is truthy");
139+
} else {
140+
console.log("the value is falsy");
141+
}
142+
Returns: "the value is falsy"
143+
144+
var isGoing = true;
145+
var color;
146+
147+
if (isGoing) {
148+
color = "green";
149+
} else {
150+
color = "red";
151+
}
152+
153+
console.log(color);
154+
//Prints: "green
155+
//TIP: Using if(isGoing) is the same as using if(isGoing === true).
156+
//Alternatively, using if(!isGoing) is the same as using if(isGoing === false).
157+
158+
159+
//The ternary operator provides you with a shortcut alternative for writing lengthy if...else statements.
160+
161+
conditional ? (if condition is true) : (if condition is false)
162+
163+
var isGoing = true;
164+
var color = isGoing ? "green" : "red";
165+
console.log(color);
166+
//Prints: "green"
167+
168+
var adult = true;
169+
var preorder = true;
170+
171+
console.log("It costs $" + (adult ? "40.00" : "20.00") + " to attend the concert. Pick up your tickets at the " + (preorder ? "will call" : "the gate") + ".");
172+
// prints: It costs $40.00 to attend the concert. Pick up your tickets at the will call.
173+
174+
/*
175+
* Programming Quiz - Navigating the Food Chain (3-8)
176+
*
177+
* Use a series of ternary operator to set the category to one of the following:
178+
* - "herbivore" if an animal eats plants
179+
* - "carnivore" if an animal eats animals
180+
* - "omnivore" if an animal eats plants and animals
181+
* - undefined if an animal doesn't eat plants or animals
182+
*
183+
* Notes
184+
* - use the variables `eatsPlants` and `eatsAnimals` in your ternary expressions
185+
* - `if` statements aren't allowed ;-)
186+
*/
187+
188+
// change the values of `eatsPlants` and `eatsAnimals` to test your code
189+
//currently not finished yet!
190+
var eatsPlants = false;
191+
var eatsAnimals = false;
192+
193+
var category = (eatsPlants? "herbivore" : "carnivore") && (eatsAnimals? "canivore": "herbivore") && (eatsAnimals && eatsPlants? "omnivore":"undefined"); /* your code goes here */
194+
195+
console.log(category);
196+
197+
/*A switch statement is an another way to chain multiple else if statements,
198+
that are based on the same value without using conditional statements.
199+
Instead, you just switch which piece of code is executed based on a value.*/
200+
201+
//example 1 as an else if
202+
var option = 3;
203+
if (option === 1) {
204+
console.log("You selected option 1.");
205+
} else if (option === 2) {
206+
console.log("You selected option 2.");
207+
} else if (option === 3) {
208+
console.log("You selected option 3.");
209+
} else if (option === 4) {
210+
console.log("You selected option 4.");
211+
} else if (option === 5) {
212+
console.log("You selected option 5.");
213+
} else if (option === 6) {
214+
console.log("You selected option 6.");
215+
}
216+
217+
//example 1 as a switch statement
218+
var option = 3;
219+
switch (option) {
220+
case 1:
221+
console.log("You selected option 1.");
222+
case 2:
223+
console.log("You selected option 2.");
224+
case 3:
225+
console.log("You selected option 3.");
226+
case 4:
227+
console.log("You selected option 4.");
228+
case 5:
229+
console.log("You selected option 5.");
230+
case 6:
231+
console.log("You selected option 6.");
232+
}
233+
234+
/*Prints:
235+
You selected option 3.
236+
You selected option 4.
237+
You selected option 5.
238+
You selected option 6.*/
239+
240+
//example 1 as a break statement
241+
var option = 3;
242+
switch (option) {
243+
case 1:
244+
console.log("You selected option 1.");
245+
break;
246+
case 2:
247+
console.log("You selected option 2.");
248+
break;
249+
case 3:
250+
console.log("You selected option 3.");
251+
break;
252+
case 4:
253+
console.log("You selected option 4.");
254+
break;
255+
case 5:
256+
console.log("You selected option 5.");
257+
break;
258+
case 6:
259+
console.log("You selected option 6.");
260+
break; // technically, not needed
261+
}
262+
//Prints: You selected option 3.
263+
264+
//Falling-through
265+
266+
var tier = "nsfw deck";
267+
var output = "You’ll receive "
268+
269+
switch (tier) {
270+
case "deck of legends":
271+
output += "a custom card, ";
272+
case "collector's deck":
273+
output += "a signed version of the Exploding Kittens deck, ";
274+
case "nsfw deck":
275+
output += "one copy of the NSFW (Not Safe for Work) Exploding Kittens card game and ";
276+
default:
277+
output += "one copy of the Exploding Kittens card game.";
278+
}
279+
280+
console.log(output);
281+
//Prints: You’ll receive one copy of the NSFW (Not Safe for Work) Exploding Kittens card game and
282+
//one copy of the Exploding Kittens card game.
283+
284+
/*
285+
* Programming Quiz: Back to School (3-9)
286+
*/
287+
288+
// change the value of `education` to test your code
289+
var education = "no high school diploma";
290+
291+
// set the value of this based on a person's education
292+
var salary;
293+
294+
// your code goes here
295+
switch (education) {
296+
case "no high school diploma":
297+
salary = ("earned an average of $25,636/year");
298+
break;
299+
case "a high school diploma":
300+
salary = ("earned an average of $35,256/year");
301+
break;
302+
case "an Associate's degree":
303+
salary = ("earned an average of $41,496/year");
304+
break;
305+
case "a Bachelor's degree":
306+
salary = ("earned an average of $59,124/year");
307+
break;
308+
case "a Master's degree":
309+
salary = ("earned an average of $69,732/year");
310+
break;
311+
case "a Professional degree":
312+
salary = ("earned an average of $89,960/year");
313+
break;
314+
case "a Doctoral degree":
315+
salary = ("earned an average of $84,396/year");
316+
break; // technically, not needed
317+
318+
}
319+
console.log("In 2015, a person with " + education + " earned an average of " + salary + ".");
320+
321+
//Example of an else if statement
322+
// change the value of `weather` to test your conditional statements
323+
var weather = "snow";
324+
if (weather === "snow") {
325+
console.log("Bring a coat.");
326+
} else if (weather === "rain") {
327+
console.log("Bring a rain jacket.");
328+
} else {
329+
console.log("Wear what you have on.");
330+
}
331+
332+
//Example of an if else statement
333+
var a = 1;
334+
var b = 2;
335+
336+
if (a > b) {
337+
console.log("a is greater than b");
338+
} else {
339+
console.log("a is less than or equal to b");
340+
}
341+
342+
//Example of an else if statement
343+
// change the value of `musicians` to test your conditional statements
344+
var musicians = 1;
345+
346+
if (musicians <= 0) {
347+
console.log("not a group");
348+
} else if (musicians == -1) {
349+
console.log("not a group");
350+
} else if (musicians == 1) {
351+
console.log("solo");
352+
} else if (musicians == 2) {
353+
console.log("duet");
354+
} else if (musicians == 3) {
355+
console.log("trio");
356+
} else if (musicians == 4) {
357+
console.log("quartet");
358+
} else {
359+
console.log("this is a large group");
360+
}
361+
362+
363+
//Javascipt for making the topbanner of your page the same height of the device
364+
//just change the '.topbanner' to the name of the class for your top banner or jumbotron.
365+
366+
<script>
367+
$(window).resize(function() {
368+
$('.topbanner').height($(window).height());
369+
});
370+
371+
$(window).trigger('resize');
372+
</script>

0 commit comments

Comments
 (0)