Day 9 (week 2) — Arrays and Loops

Eric Abell
Jul 27, 2017 · 3 min read

Today, we did something different. The cohort was broken into two groups. The first group spent more time reviewing the material from last night’s homework while the second group just went ahead and started the morning pair programming activities. This is an example of differentiated instruction.

Our group of four started on the pair programming challenges. These were pretty simple. Here’s an example.

3. Create a string that, when printed to the console, will output a triangle of stars. The triangle should look like this:*
**
***
****
*****

Here’s my solution:

function printTriangle(){
let row = "";
let triangle = "";
for( let i=0; i < 5; i++ ) {
row += "*";
triangle += row + "\n";
}
return triangle;
}

Normally, I might do this with two embedded for loops, but this solution uses only one loop.

These were all fairly easy, although I did get hung up on one of them. The interesting part for me was that I used map, reduce, and filter where possible. I think the one I am most proud of is this one.

Given a list of numbers, multiply each by the index value and the sum the list.

My first solution was two subsequent for loops, which looked something like this

let numberArray = [ 23, 234, 64 , 89, 2, 756, 3, 58, 674, 32, 756, 23, 6 ];function getBigNumber(){
// multiply each number by the index
for(let i=0;i<numberArray.length;i++) {
numberArray[i] *= i;
}
let sum = 0;
// sum the list
for(let i=0;i<numberArray.length;i++) {
sum += numberArray[i];
}
return sum;
}

This works, but I found a much shorter and I think effective way using reduce. The only thing I don’t like about it is that I have to subtract the first element from the sum, since it was never multiplied by 0.

function getBigNumber() {
return( numberArray.reduce(function(e1,e2,i) {
return e1 + i*e2; }) - numberArray[0]
)
}

Notice that the callback to reduce is taking three parameters — two elements from the list and the index of the second element. This is why the first element never gets multiplied by 0.

The addition of a console.log statement in the callback helps to see what e1, e2, and i are set to.

23 234 1 // first run, 23 and 234 are first two elements, i=1
257 64 2 // second run, 257=234+23 and i=2
385 89 3 // third run, 385=257+64*2
652 2 4 // fourth run, 652=385+89*3
660 756 5 // ...
4440 3 6
4458 58 7
4864 674 8
10256 32 9
10544 756 10
18104 23 11
18357 6 12 // now we just have to subtract 23 from 18357

After lunch, we started on the daily programming challenge. I finished this in just under 20 minutes. I started on the weekly project next.

The weekly project is to take some sample data from Etsy.com and selectively answer some questions about calculating average price or getting all the items made of wood. My solution is available here.

With both the daily and weekly project completed, I’ve got some time to spend working on some other coding challenges. I’ve also been doing some reading at home about machine learning and AI development.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade