Udemy. section 11 Arrays.

Challenge #1

by trentHarlem

HTML

<h1>
//////////////////////////////////////<br>
// Coding Challenge #1
</h1>
<p>
Julia and Kate are doing a study on dogs. So each of them asked 5 dog owners about their dog's age, and stored the data into an array (one array for each). For now, they are just interested in knowing whether a dog is an adult or a puppy. A dog is an adult if it is at least 3 years old, and it's a puppy if it's less than 3 years old.
<br><br>
Create a function 'checkDogs', which accepts 2 arrays of dog's ages ('dogsJulia' and 'dogsKate'), and does the following things:
<br><br>
1. Julia found out that the owners of the FIRST and the LAST TWO dogs actually have cats, not dogs! So create a shallow copy of Julia's array, and remove the cat ages from that copied array (because it's a bad practice to mutate function parameters)<br><br>
2. Create an array with both Julia's (corrected) and Kate's data<br><br>
3. For each remaining dog, log to the console whether it's an adult ("Dog number 1 is an adult, and is 5 years old") or a puppy ("Dog number 2 is still a puppy ๐Ÿถ")<br><br>
4. Run the function for both test datasets
<br>
HINT: Use tools from all lectures in this section so far ๐Ÿ˜‰
<br><br>
TEST DATA 1: Julia's data [3, 5, 2, 12, 7], Kate's data [4, 1, 15, 8, 3]<br><br>
TEST DATA 2: Julia's data [9, 16, 6, 8, 3], Kate's data [10, 5, 6, 1, 4]<br>

GOOD LUCK ๐Ÿ˜€
</p>

CSS

body {
  font: 1.1em system-ui;
}

JavaScript

// 'use strict'; // doesn't like console.log()

// create function checkDogs 
//      that accepts 2 arrays
//const checkDogs= function (dogsJulia, dogsKate) {
const checkDogs = function(arr1, arr2) {

  // 1. create copy of Julia's data / arr1 and remove cats // ( first and last two elements)

  const dogsJulia = arr1.slice(1, -2);
  const dogsKate = arr2;

  // 2. create array with both args (corrected)

  const arrCombo = [...dogsJulia, ...dogsKate]
  console.log(dogsJulia, dogsKate)
  console.log(arrCombo)

  // 3. log each result to console.

  arrCombo.forEach(function(age, i) {
    // with ternary operator
    age >= 3 ?
      console.log(`Dog number ${i+1} is an adult and is ${age} years old.`) :
      console.log(`Dog number ${i+1} is still a puppy.`);

    // with if statement
    /*     if (age > 3) {
          console.log(`Dog number ${i+1} is an adult and is ${age} years old.`)
        } else {
          console.log(`Dog number ${i+1} is still a puppy.`)
        } */

  })
};

// 4. run function for both data sets

// test data 1: 
//checkDogs([3, 5, 2, 12, 7],[4, 1, 15, 8, 3]);

// test data 2:
checkDogs([9, 16, 6, 8, 3], [10, 5, 6, 1, 4])