-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_exercises.html
More file actions
215 lines (155 loc) · 4.57 KB
/
js_exercises.html
File metadata and controls
215 lines (155 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<!DOCTYPE html>
<html>
<head>
<title>Javascript Example</title>
</head>
<body>
<script>
// JAVASCRIPT INTRO!!
// RUBY CONSOLE:
// puts "hello"
// JS CONSOLE:
// console.log("hello");
// RUBY PROMPT:
// puts "What is your name?"
// username = gets.chomp
// JS PROMPT:
// var username = prompt("What is your name?");
// console.log(username);
// RUBY VARIABLES:
// x = "hello"
// puts x
// JS VARIABLES
// var x = "hello";
// console.log(x);
// RUBY CONCAT:
// x = "cat"
// y = "dog"
// puts "#{x}#{y}"
// JS CONCAT:
// var x = "cat";
// var y = "dog";
// console.log(x + y);
// RUBY NUMS:
// x = 1
// y = 2
// z = 4.34
// puts x + y
// puts x + z
// JS NUMS:
// var x = 1;
// var y = 2;
// var z = 4.34;
// console.log(x+y);
// console.log(x + z);
// RUBY INC/DEC:
// x = 1
// x += 1
// x -= 1
// JS INC/DEC:
// var x = 2;
// x++;
// console.log(x);
// x--;
// console.log(x);
// RUBY BOOLS:
// x = true
// y = false
// puts x
// puts y
// JS BOOLS:
// var x = true;
// var y = false;
// console.log(x, y);
// RUBY ARRAYS:
// people = ["Wen", "Dani", "Annie"]
// puts people[0]
// puts people[1]
// people << "Jenilee"
// puts people
// puts people.count
// JS ARRAYS:
// var people = ["Wen", "Dani", "Annie"];
// console.log(people[0]);
// console.log(people[1]);
// people.push("Jenilee");
// console.log(people.length);
// // INDEX OF ELEMENT IN ARRAY JS:
// console.log(people.indexOf("Wen"));
// RUBY HASHES
// person = {first_name: "John", last_name: "Doe", eye_color: "Brown", age: 40}
// puts person[:first_name]
// x = :first_name
//puts person[x]
// JS OBJECTS:
// var person = {first_name: "John", last_name: "Doe", eye_color: "Brown", age: 40};
// console.log(person.first_name);
// console.log(person['first_name']);
// var x = "first_name";
// console.log(person[x]);
// console.log(person.x); DOESN'T WORK!
// RUBY UNDEFINED:
// x
// puts x
// JS UNDEFINED:
// var x;
// console.log(x);
// RUBY TYPE:
// puts "Wen".class => String
// puts 1.class => Integer
// puts 1.23.class => Float
// puts false.class => FalseClass
// puts {name: "wen"}.class => Hash
// JS TYPE:
// console.log(typeof "Wen"); // string
// console.log(typeof 2.15); // number
// console.log(typeof false); //boolean
// console.log(typeof [1,2,3]); //object
// console.log(typeof {name: "wen"}); //object
// RUBY TYPE CONVERSION:
// "234".to_i
// 234.to_s
// JS TYPE CONVERSION:
// console.log(parseInt("234"));
// var num = 234;
// console.log(num.toString());
// Exercise
//create an array of three “pets” and output the second pet to the console.
var pets = ["dog", "fish", "cat"];
console.log(pets[1]);
// add a 4th pet on a new line without changing the code of your original array.
pets.push("hamster");
console.log(pets);
// With your pets array you’ve just created, how can you get returned the index position of the last pet you added?
console.log(pets.indexOf("hamster"));
//what gets returned if you search for the index of a pet that isn’t in your pets array?
console.log(pets.indexOf("chicken"));
//returns -1
//Create an object that represents a particular book, having properties of title, author, isbn, pages, and year of publication. Now turn “book” into an array of “books” (add a second book).
var books = [{
title: "The Girl on the Train",
author: "Paula Hawkins",
isbn: "9781594634024",
pages: "336",
year: "2016"
},
{
title: "Be Before You",
author: "Jojo Moyes",
isbn: "9780143130154",
pages: "480",
year: "2012"
}];
// Add a 3rd book a new line without changing your code of “books”.
var newbook = {
title: "Gone Girl",
author: "Gillian Flynn",
isbn: "9780307588371",
pages: "432",
year: "2014"
};
books.push(newbook);
console.log(books);
</script>
</body>
</html>