Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidystephenson/1890a5e88a8356e34a978cbad765dff3 to your computer and use it in GitHub Desktop.
Save davidystephenson/1890a5e88a8356e34a978cbad765dff3 to your computer and use it in GitHub Desktop.
<script>
// 1. Create a variable `favoriteFruits` and assign it an array of strings representing your favorite fruits.
var favoriteFruits = ['apples', 'grapes', 'olives']
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]
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'
}
console.log(employee)
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 = {}
console.log(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'
}
]
console.log(employees)
for (const 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 letters = ['a', 'b', 'c','d', 'e', 'f']
// letters.reverse()
// console.log(letters)
var reversed = []
for (var i = letters.length - 1; i >= 0; i--) {
console.log('i', i)
var letter = letters[i]
console.log('letter', letter)
reversed.push(letter)
}
console.log(reversed)
console.log("\n-------------------------------------------------\n");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment