Leanna Garner

Adventures in tech

Say hello to them!

Asterope
Asterope
apple keyboard and magic mouse

aAO Array Fun

I’ve decided to get back on with app/Academy Open. I figured, if I’m spending time looking for a job, almost obsessively, why not spend time learning about useful skills that I could eventually bring to them as well? We’ve got the ability to learn new things. Right now, I’m working through a/AO’s “JavaScript Fundamentals – Intermediate Arrays” part.

Going back to the job hunt happenings: there are bound to be local businesses that need people that have the knowledge to help them out, but they just might not be posting jobs or hiring at the moment.

I believe I missed the summer internship window for the most part, but I’ve been putting in applications where I can, since it looks like there’s some happening in the fall. Whether or not I score one, I can still study and build things on my own.

Array Exercises I’ve Found Fun

There were a few exercises in the “Add to Arrays” section that I’m working through that were fun to try to solve. Here are the ones I’ve enjoyed the most.

Greatest Factor

For this exercise, you have to add an element to an array while you iterate through one passed in as an argument. Depending on if the element at the index is odd or even:

  • Even numbers must have their largest factor added to the new array
  • Odd numbers are simply added to the new array.

I would’ve done more initial work to give the greatest factor upfront, but in this problem’s case, I didn’t have to worry about finding an odd number’s largest factor.

The answer for this is easy, though.

The factor multiplied by two will give the even number, so for this specific problem, all you have to do is divide the element in the array by two, then push it; if it’s odd, just push it directly to the new array!

function greatestFactorArray(array) {
  let facArr = [];
  for (let i = 0; i < array.length; i++) {
    if (array[i] % 2 === 0) {
      facArr.push(array[i] / 2);
    } else {
      facArr.push(array[i]);
    }
  }
  return facArr;
}
JavaScript

However, if you want to account for finding an odd number’s greatest factor, you could do this:

function greatestFactor(num) {
  for (let i = Math.floor(num / 2); i > 0; i--) {
    if (num % i === 0) {
      return i;
    }
  }
}
JavaScript

I used the Math.floor() function to find the nearest integer, going down. I recall going over this function (along with “ceiling”, which is the nearest integer, going up) from Discrete Math.

For an odd number, dividing by two would result in something that’s not a whole number, so the floor function lets me “round” it down.

Summation Sequence

This took a little bit of thinking at first. You have two arguments: the first one being the starting number, and the second one is for how many elements total should be in the new array. For example, if we have the arguments passed in as 3, 4, then in the array, we should have four elements:

[3, 6, 21, 231]

It’s the same idea as the “find the sum of all natural numbers between 1 and 100”, but for the first number, in this case, 3, you would do 3 + 2 + 1, and then the sum of that, 6, would be pushed to the new array.

Then on your second iteration, you’ll do 6 + 5 + 4 … + 1 and then add that sum, 21, to the array, then move on. Do you see the pattern?

I believe this is an arithmetic series, is it not?

n\left(
\frac{a_1 + a_n}{2}
\right)

If we’re using 21, then that would be:

21\left(
\frac{21 + 1}{2}
\right) = 21(11) = 231

Which is what’s expected. But I don’t think a/AO expected for people to figure out the formula, which would be easier to write… and it would be less work for the computer.

Before I veer too far out with the math-heavy side, I’ll share what I wrote as my solution:

function summationSequence(start, length) {
  let arr = [start];
  for (let i = 0; i < length - 1; i++) {
    let sum = 0;
    for (let j = arr[i]; j > 0; j--) {
      sum += j;
    }
    arr.push(sum);
  }
  return arr;
}
JavaScript

On the first run of this program, I didn’t have the length - 1 portion, so it gave me the sum of 231 + 230 + 229… and so on. I pondered why this was the case, but I learned that because the first element (the value of start ) is already in the array, I would have to iterate this one less than the length provided.

Now, using the above formula, I could easily write it as this, which would give me the same output, and use only one loop instead of one nested in the other.

function summationSequence(start, length) {
  let arr = [start];
  for (let i = 0; i < length - 1; i++) {
    let sum = (arr[i] * ((1 + arr[i]) / 2))
    arr.push(sum);
  }
  return arr;
}
JavaScript

It’s actually fun to see these formulas in code. Math ties into computer science. I think if we can all approach it softly, with open arms, maybe we wouldn’t dislike it so much.


The next thing on the curriculum is their “Advanced Arrays”, which I’ve started but got caught up with rewriting my resume and looking for new internships and entry level jobs to apply to. I’ll definitely make a post on that, since there’s an exercise that apparently has an estimated completion time of an hour and a half… It must be a challenge, and I’m looking forward to overcoming it!

(And for this post… it was a small challenge to get the LaTeX math code to show up. It’s easy in Obsidian; all you have to do is nest them in a pair of $, or for a separate block, in a pair of $$. I ended up using the KaTeX WordPress plugin which allows me to simply type in what would be in between the $ signs.)

Here’s to having luck in the job hunt, and to sticking with working through a/AO’s curriculum!