This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Book(config) { | |
this.title = config.title; | |
this.author = config.author; | |
this.numPages = config.numPages; | |
this.currentPage = 0; | |
} | |
let newBook = { | |
title: "Robot Heart", | |
author: "J.K.", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// code inside ingrediend function can see the parameter of hummus function | |
// but outer - hummus function cannot see the inner bindings of ingredient function | |
const hummus = function(factor) { | |
const ingredient = function(amount, unit, name) { | |
let ingredientAmount = amount * factor; | |
if (ingredientAmount > 1) { | |
unit += "s"; | |
} | |
console.log(`${ingredientAmount} ${unit} ${name}`); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//To create an object(3): | |
//1) | |
var obj = new Object(); | |
//2) | |
var obj = {}; | |
//3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] | |
for i in a: | |
if i % 2 == 0 and i != 0: | |
print(i) | |
# Output: | |
# 2 | |
# 4 | |
# 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
webster = { | |
"Aardvark" : "A star of a popular children's cartoon show.", | |
"Baa" : "The sound a goat makes.", | |
"Carpet": "Goes on the floor.", | |
"Dab": "A small amount." | |
} | |
for i in webster: | |
print(webster[i]) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
start_list = [5, 3, 1, 2, 4] | |
square_list = [] | |
for i in start_list: | |
test = i ** 2 | |
square_list.append(test) | |
square_list.sort() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nums = [1,2,3,4,5,6,7,8,9,10] | |
my_list = [] | |
for n in nums: | |
my_list.append(n) | |
print(my_list) | |
=>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
my_list = [n for n in nums] | |
print(my_list) | |
=>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list = list() | |
list = list([2,3,4]) | |
Traceback (most recent call last): | |
File "<input>", line 1, in <module> | |
TypeError: 'list' object is not callable | |
list = [] | |
list.append(2) | |
list.append(3, 4) | |
Traceback (most recent call last): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NUMBER_OF_ELEMENTS = 5 | |
numbers = [] | |
sum = 0 | |
for i in range(NUMBER_OF_ELEMENTS): | |
value = eval(input("Enter a new number: ")) | |
numbers.append(value) | |
sum += value | |
average = sum / NUMBER_OF_ELEMENTS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def capitalizeEach(string): | |
print(" ".join(w.capitalize() for w in string.split())) | |
capitalizeEach("This is a loooog string") | |
NewerOlder