- Print blue and 1996 to the screen using object destructuring
const car = {
colour: 'blue',
brand: 'ford',
gears: 4,
year: '1996',
def sum_with_for(min, max) | |
total = 0 | |
# we create the total variable outside of the loop so that we can use it for the sum, we add to it in each iteration | |
return -1 if min > max | |
# we can put the return -1 here as we can check the values at this point even before the first loop | |
for i in (min..max) do | |
total += i | |
end | |
# for each loop we add total to i (i represents the iteration) | |
# example => total = total + 1 on the first loop (total is set at zero), then total = total + 2 on the second loop (total will now be 1 after that first iteration) |
So far we have mostly been using HTML buttons to trigger JS functions, but this is only one event we can use to trigger logic from our JS files. In previous tasks we set up what is called an event listener to detect when the button was clicked. We passed a callback as a function that gets called when event is triggered. Being able to pass functions as parameters - the same way as we can with variables - is one of the major strengths of the JS language.
Event-based programming can be a little tricky to wrap your head around, especially as a beginner. Up until now you have probably been mainly programming in a sequential or imperative way - each line is executed one after another. When we introduce event listeners and callbacks we are determining the functionality we want to happen but we are not actually in control of when this functionality happens - functions aren't called until the button is actually clicked by the user.
Let's create a Coder Academy webapp that allows users t
var findAndUpdate = function(personName, done) { | |
// need a bit more clarification on what the done function is actually doing | |
var ageToSet = 20 | |
const person = Person.findOneAndUpdate({name: personName}, {age: ageToSet}, {new: true}, function(err, data){ | |
if (err) { | |
return done(err) | |
} | |
else { | |
return done(null, data) | |
// why do we pass null as the first argument in the success method call |
This is a chance to implement a full MERN stack app, and get some practice with all the pieces of technology we have used to date.
I know we went through how to deploy with now.sh
and this works most of the time. In saying that, I've lost a bit of faith in Now over the past few cohorts seeing some weird bugs.
Now used to be great for Express deployments but it has changed a lot over the past year.
You can see what kinds of deployments Now is great for by running:
const express = require('express') | |
const router = express.Router() | |
router.use('/users', require('./users')) | |
module.exports = router |
# board begins its life as an array of empty strings | |
board = [" ", " ", " ", " ", " ", " ", " ", " ", " "] | |
# method for displaying the tictactoe board | |
def display_board(board) | |
puts " 1 2 3" | |
puts "A #{board[0]} | #{board[1]} | #{board[2]}" | |
puts " ----------" | |
puts "B #{board[3]} | #{board[4]} | #{board[5]}" | |
puts " ----------" |