xxxxxxxxxx
99
/*
Question:
In JavaScript, what's an array?
Answer:
An ordered list that can contain a mix of different data types,
including numbers, strings, and Booleans.
(It's like a column in a spreadsheet.)
*/
// creating arrays
let myArray = [2, 3, 5];
// accessing array elements
console.log(myArray[0]); // 2 (the array element at index 0)
console.log(myArray[1]); // 3 (the array element at index 1)
console.log(myArray[2]); // 5 (the array element at index 2)
// length of an array
console.log(myArray.length); // 3 (the array has three elements)
// modifying array elements
myArray[0] = 1; // note that myArray is now [1, 3, 5]
// add a new element, 7, to the end of myArray
// (can also do myArray[3] = 7;)
myArray.push(7);
console.log(myArray); // [1, 3, 5, 7]
/*
Exercise:
Create an xy-table of values for the equation y = x ** 2,
as a JavaScript array. Use x-values -2, -1, 0, 1, and 2.
*/
let xyTable = [[-2, 4], [-1, 1], [0, 0], [1, 1], [2, 4]];
console.log(xyTable[0]); // [-2, 4]
console.log(xyTable[0][1]); // 4
/*
In a JavaScript array, store the daily high temperatures from
August 25-31, 2024 in the Denver area: 94, 88, 86, 92, 86, 87, 91
Source: https://www.weather.gov/wrh/Climate?wfo=bou
*/
let highTemps = [94, 88, 86, 92, 86, 87, 91];
/*
Exercise:
Compute the average of the temperatures in highTemps by "looping over
the array." Log the result to the console.
Remark:
If this were data for the whole month, a loop
would be much more convenient than a hardcoded sum.
Note to self:
This is good practice with remembering that the length of the
array and the last index in the array are not the same.
*/
/* Bonus exercise:
Previously, we wrote a computer program that predicted an item of food
would expire in 1.698970004 weeks.
For fun, can you convert this value to the format
"w weeks, d days, h hours, m minutes, s seconds"?
For example, 9 days would be written as
"1 week, 2 days, 0 hours, 0 minutes, 0 seconds".
Try to write a computer program that automates the repetitive steps
and outputs a formatted time. As always, try out your code to see if
it works!
Hints (I should maybe put these in a separate file):
1. Think about how you'd do the computation by hand. What's
the same in each step? What's different?
2. The part that gets repeated over and over can go inside a loop.
3. Each step uses a different conversion factor. You can put the
conversion factors in an array and loop over the array, so that
each step can use a different value.
*/
/*
Questions that we'll address once we learn a little more:
1) What's a good way to avoid using such a long line of code
when creating the formatted string?
Programming a loop of some sort? Or create an "object"?
2) Could we make some kind of reusable singular/plural adjuster?
*/