Activity: Arrays (Advanced)
Note: Keep and don't delete these programs you write in this activity, we will re-use them in future lessons! One thing you can do (if you haven't done so already) is create a repl.it account, login, and save your work as you complete the following problems.
Find min & max within an array
Given an array, write a program that returns the largest element. Then try the smallest element. Hint: this will require variables, loops, and conditionals!
const arr = [25, 101, 66, 10, 99]
// for the array above, expect:
// largest element => 101
// smallest element => 10
Detect if an element is within an array
Given an array and a target value, write a program that returns whether or not the target value exists within the array. Hint: what type will the result value be? What should its default value be?
let result; // what should this be set to by default?
const arr = [25, 101, 66, 10, 99]
let target = 66 // result should be "true"
target = 77 // result should be "false"
Build a simple array
Given an integer size, write a program that builds an array of that size where each element is its own index.
const integerSize = 10;
// expect array to be: [0,1,2,3,4,5,6,7,8,9]
- Try with each element value being double its own index? Or triple?
- What about if all the odd indices are double while all the even indices are quadruple?
Range of numbers
Given two integers less than 1000, make an array with all the numbers between the two integers (inclusive).
// Example input:
const lowerBound = 20
const upperBound = 25
// Expected Output: [20,21,22,23,24,25]
CS/Math Question: How large will the array be with a lowerBound of X and an upperBound of Y?
Average value
Given an array of numbers, find the average (mean) value. You can do this by adding up all the numbers and then dividing by how many you added together.
// Example input:
const inputArray = [5, 15, 10, 30, 25]
// Expected Output: 17