Skip to content

Commit 34a4e88

Browse files
committed
JavaScript course coding exercise solutions
0 parents  commit 34a4e88

File tree

6 files changed

+77
-0
lines changed

6 files changed

+77
-0
lines changed

exercise_1.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function return_only_10_plus(){
2+
let initial_array = [1, 21, 5, 6, 42, 8, 3, 10, 15]
3+
4+
//Populate the final array only with numbers greated than 10
5+
let final_array = []
6+
7+
final_array = initial_array.filter(function(elem){
8+
return elem > 10;
9+
})
10+
return final_array
11+
12+
}

exercise_2.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function greeting(message){
2+
return "The message is: " + message;
3+
}

exercise_3.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//You only need one function in your object - fly()
2+
let Bird = {
3+
fly: function(){
4+
return true;
5+
}
6+
}

exercise_4.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<div id="challenge">Can you change me?</div>
2+
3+
<script>
4+
var elem = document.getElementById("challenge");
5+
elem.textContent = 'I did it!'
6+
</script>

exercise_5.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function findTheObject() {
2+
let investments = [
3+
{
4+
name: 'First',
5+
price: 0.50,
6+
type: 'Bond'
7+
},
8+
9+
{
10+
name: 'Second',
11+
price: 1.75,
12+
type: 'ETF'
13+
},
14+
15+
{
16+
name: 'Third',
17+
price: 1.50,
18+
type: 'Stock'
19+
}
20+
];
21+
return investments.filter(object => object.type == 'Stock');
22+
}

exercise_6.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
my_stocks = { 'Apple' => { 'symbol' => 'AAPL', 'price' => 100 },
2+
'Google' => { 'symbol' => 'GOOG', 'price' => 1150 },
3+
'Tesla' => { 'symbol' => 'TSLA', 'price' => 295 },
4+
'Microsoft' => { 'symbol' => 'MSFT', 'price' => 95},
5+
'Netflix' => { 'symbol' => 'NFLX', 'price' => 300},
6+
'Facebook' => { 'symbol' => 'FB', 'price' => 175},
7+
'Amazon' => { 'symbol' => 'AMZN', 'price' => 1250} }
8+
9+
def execute_exercise(my_stocks)
10+
# 1) Print out all the symbols and prices, as they are stored as values in the hash above, using for loop
11+
# Write your code below here, I have written this one for your convenience
12+
for i, j in my_stocks
13+
puts j
14+
end
15+
16+
# 2) Print out all the symbols and prices if the price of the stock is above 500
17+
# Write your code below here
18+
for i, j in my_stocks
19+
puts j if j["price"] > 500
20+
end
21+
22+
# 3) Same task as above using .each
23+
# Write your code below here, don't write past the end statement at the bottom
24+
my_stocks.each do |i, j|
25+
puts j if j["price"] > 500
26+
end
27+
28+
end

0 commit comments

Comments
 (0)