Skip to content

Instantly share code, notes, and snippets.

@lauracodecreations
Created November 16, 2018 23:39
Show Gist options
  • Save lauracodecreations/30b673f9019f5fb1d16e2452a4ad8f84 to your computer and use it in GitHub Desktop.
Save lauracodecreations/30b673f9019f5fb1d16e2452a4ad8f84 to your computer and use it in GitHub Desktop.
js exercises
// Question 1:
const personAge = 55
const adaAge = 2
// person_age = 55
// ada_age = 2
//
// if person_age < ada_age
// print "This person is younger"
// elsif ada_age < person_age
// print "Ada is younger"
// else
// print "They’re the same!"
// end
if (personAge < adaAge) {
console.log("This person is younger")
} else if (adaAge < personAge) {
console.log ("Ada is younger")
} else {
console.log("They are the same")
}
// Question 2:
// x = 7
// y = 7
//
// if x > y || x == y
// if x > y
// print "x is bigger"
// else
// print "x = y"
// end
// else
// print "y is bigger"
// end
const x = 7
const y = 7
if ( x < y ) {
if ( x > y) {
console.log("x is bigger")
} else {
console.log( "x = y")
}
} else {
console.log("y is bigger")
}
//Question 3
// 10.times do |i|
// puts i * i
// end
// let i = 0;
// while (i < 10) {
// console.log(i * i);
// i += 1;
// }
// Question 4
// total = 0
//
// (1..3).each do |i|
// total = total + i
// end
//
// puts total
let total = 0
for ( let i = 1; i < 4; i++ ) {
total = total + i
}
console.log(total)
//Question 5
// while i < 3
// puts "hi"
// i = i + 1
// end
//
// puts "bye"
// let i = 0
// while ( i < 3 ) {
// console.log("hi");
// i += 1;
// }
// console.log("bye")
// Question 6
const fruits = ["banana", "apple", "kiwi"]
for (let fruit of fruits) {
console.log(`I love ${fruit}`);
}
// fruits.each do |fruit|
// puts "I love #{fruit}!"
// end
//Question 7
// Ruby
//total = 0
// values = [4, 6, 2, 8, 11]
//
// values.each do |value|
// total = total + value
// end
//
// puts total
// JS:
//let total = 0
//const values = [4, 6, 2, 8, 11]
//
// for (let value of values) {
// total = total + value
// }
//
// console.log(`${total}`)
// Question 8
const values = [8, 5, 3, 10, 14, 2]
for (let value of values) {
if (value == 10) {
console.log("Special case!")
} else {
console.log(`regular values like ${value}`)
}
}
// values.each do |value|
// if value == 10
// puts "Special case!"
// else
// puts "Regular values like #{value}"
// end
// end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment