Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidystephenson/d6690838c91b8a7e22239a391a6e49ee to your computer and use it in GitHub Desktop.
Save davidystephenson/d6690838c91b8a7e22239a391a6e49ee to your computer and use it in GitHub Desktop.
// 1. Create a variable `favoriteFruits` and assign it an array of strings representing your favorite fruits.
var favoriteFruits = ['apples', 'olives', 'grapes']
console.log(favoriteFruits)
console.log("\n-------------------------------------------------\n");
// 2. Create a variable `mixedArray` that contains a mix of data types, including numbers, strings, and booleans.
var mixed = [1, 'hello', true]
console.log(mixed)
console.log("\n-------------------------------------------------\n");
// 3. Write a program that iterates through each number of an array using for..of loop and computes the sum of squares of each of these numbers.
var numbers = [1, 2, 3]
var result = 0
for (var number of numbers) {
var square = number * number
result += square
}
console.log(result)
console.log("\n-------------------------------------------------\n");
// 4. Write a program to find maximum number in a numeric array.
var numbers = [1, 3, 2, 5, 0]
var max = numbers[0]
for (var number of numbers) {
if (number > max) {
max = number
}
}
console.log(max)
console.log("\n-------------------------------------------------\n");
// -------------------------------------------------------------------------------------------------------------------------------------------------------
// 5. Declare an object named "person" with properties "name", "age", and "city" and set their respective values to
// "John", 30, "New York" and hobbies as "reading", "swimming", "traveling". Print the name, age, and city of the person.
var person = {
name: 'John',
age: 30,
city: 'New York',
hobbies: ['reading', 'swimming', 'traveling']
}
console.log(person.name)
console.log(person.age)
console.log(person.city)
console.log("\n-------------------------------------------------\n");
// 6. Declare an object named "employee" with properties "name", "age", and "city". Delete the "city" property from the object.
var employee = {
name: 'John',
age: 30,
city: 'New York'
}
delete employee.city
console.log(employee)
console.log("\n-------------------------------------------------\n");
// 7. Declare an object named "toy" with an empty object as its initial value.
// Add the properties "name" and "category" with values "Super Space Rocket" and "Action Figures & Playsets" respectively.
var toy = {}
toy.name = 'Super Space Rocket'
toy.category = 'Action Figures & Playsets'
console.log(toy)
console.log("\n-------------------------------------------------\n");
// 8. Write a program to create an array of objects "employees" with properties "name", "age", and "city" and set their respective values to
// "John", 30, "New York", "Thomas", 40, "Chicago", "Lily", 35, "San Francisco". Print the name, age, and city of each employee.
// Print the name, age, and city of each employee using for..of loop.
var employees = [
{ name: 'John', age: 30, city: 'New York' },
{ name: 'Thomas', age: 40, city: 'Chicago' },
{ name: 'Lily', age: 35, city: 'San Francisco' }
]
for (var employee of employees) {
console.log(employee.name)
console.log(employee.age)
console.log(employee.city)
}
console.log("\n-------------------------------------------------\n");
// 9. Write a program to reverse an array
var people = ['John', 'Thomas', 'Lily']
var reversed = []
for (var person of people) {
console.log('before', reversed)
reversed.unshift(person)
console.log('after', reversed)
}
console.log(reversed)
console.log("\n-------------------------------------------------\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment