Activity: Functions

What's wrong with this code? List out every problem:

const 3TimesArgument = func(argument) {
  arg * 3 return
}

Finish this function:

A perfect square is defined as a number that has a square-root that is a whole number. For example:

  • 9 has a square root of 3, which is a whole number. It is a perfect square.
  • 16 has a square root of 4, which is a whole number. It is a perfect square.
  • 2 has a square root of approximately 1.414, which is NOT a whole number. It is NOT a perfect square.

I set up the conditional part of the isPerfectSquare function. It works because if the square-root of a number and the rounded down value of that square-root is the same then it's a whole number, and thus the input number is a perfect square.

const isPerfectSquare = function(arg) {
  // Some code belongs here.
  // Hint: what variable needs to be created and assigned?
  if(Math.floor(sqrtOfArg) == sqrtOfArg) {
    // It's a perfect square!
  } else {
    // It's not
  }
}

isPerfectSquare(9); // returns true
isPerfectSquare(16); // returns true
isPerfectSquare(2); // return false

Fill out the function such that it can be used anywhere to determine if a number is a perfect square or not. Test it by running it many times with both numbers that are and aren't perfect squares.

Multiples of numbers

Write a function that takes as input a number, and returns a list of the first 20 multiples of that number.

// Input: 4
// Output: [4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80]
// Input: 17
// Output: [17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255, 272, 289, 306, 323, 340]

Bonus: How would you change this function to return a list of the first 10 powers of that number? Hint: Check out the JavaScript pow() Method.

What does this function do?

Please do NOT put this into repl.it, I do NOT want you to actually run it until you have figured it out! Trust me, you'll appreciate the practice. Instead test yourself by stepping through it line by line yourself. I suggest taking out a pen(cil) and paper or using sublime as a notepad to keep track of variable values as you go line by line

const MysteryFunction = function(input1, input2) {
  return MysteryFunction2(input1) + MysteryFunction2(input2)
}

const MysteryFunction2 = function(input) {
  if (input > 10) {
    return 10
  } else {
    return input + 10
  }
}

// What does it log?
console.log(MysteryFunction(15, 7))

Let's try a harder one

const MysteryFunction = function(input) {
  if (MysteryFunction2(input % 5) > 10) {
    return MysteryFunction2(input % 3)
  }
  return MysteryFunction2(input % 4)
}

const MysteryFunction2 = function(input) {
  return input * 3
}

// What does it log?
console.log(MysteryFunction(23))

Here's one with loops

const MysteryFunction = function(input) {
  if (input.length > 4) {
    return MysteryFunction2(input, 3)
  }
  return MysteryFunction2(input, 2)
}

const MysteryFunction2 = function(input1, input2) {
  for (let i = input2; i >= 0; i--) {
    input1.push(i)
  }
  return input1
}

// What does it log?
console.log(MysteryFunction([4,5,6]))
// What about this call?
console.log(MysteryFunction([10,9,8,7,6])