Giter VIP home page Giter VIP logo

functions-scope-hw's Introduction

ga-logo

Problem solving with Functions

Duration: "3:00 - 4:00"
Type: Homework
Class: SEI
Creator: GA Instructional Team
Topics: Problem solving with functions


Setup

Fork/clone this repo as directed by your instructor. Inside the repo, create the usual folder structure. Write your answers in app.js.

1. Verbal questions

Write answers to the following questions as comments.

  1. What is the difference between a parameter and an argument?
  2. Within a function, what is the difference between return and console.log?
  3. What are the implications of the ability of a function to return a value?

2. Palindrome.

Write a function checkPalindrome that accepts a single argument, a string. Later in this assignment we're gonna beef up our palindrome function some. The function should return true if the string is a palindrome, false if not. A palindrome is a word that is the same spelled backwards and forwards, for example: Racecar. Make sure your function will give the correct answer for words with capital letters.

const wordIsPaindrome = checkPalindrome("Radar");

console.log(wordIsPaindrome);
=> true
const wordIsPaindrome = checkPalindrome("Cat");

console.log(wordIsPaindrome);
=> false

3. Check a List

Write a function checkForStudent that takes 2 parameters, a student's name and an array of students, and checks if the student's name is in the array. If the student's name is included in the array the function should return true. If the name is not included in the array the function should return false. There is more than one way to do this challenge. For extra practice see if you can find at least two ways to solve it.

const studentIncluded =  checkForStudent('Sally', ['Sally', 'Kyle']);

console.log(studentIncluded);
=> true
const studentIncluded =  checkForStudent('Ron', ['Sally', 'Kyle']);

console.log(studentIncluded);
=> false

4. Sum Array

Write a function sumArray that takes an array as an argument. The array should contain numbers. The function should return the sum of the numbers in the array.

Try solving it using a loop instead of using .reduce().

Expected result:

const sum = sumArray([1, 2, 3, 4, 5, 6]);

console.log(sum);
=> 21

CSS

Watch the following three videos on CSS:

You will need to have watched these videos for tomorrow's lesson.

Hungry for more?

Digit Sum

Write a function sumDigits that accepts a number and returns the sum of its digits.

const sum = sumDigits(42);

console.log(sum);
=> 6;
Commit.

Pythagoras

Write a function calculateSide that takes two arguments: sideA and sideB, and returns the solution for sideC using the Pythagorean theorem.

hint: discover the Pythagorean Theorem on a website called google.com

hint: checkout the Math methods in javascript

For this problem, do not use Math.hypot()

const sideC = calculateSide(8, 6);

console.log(sideC);
=> 10
Commit.

Prime Numbers

A Prime number is a number that is not evenly divisible by another number except 1 and itself. If you want to read more deeply about it, go here. To test whether a number is Prime, you only need to test as far as the square root of that number. This is advisable for optimization and testing large numbers.

Commit.

Step One

Write a function called checkPrime that will test whether a number is Prime. The function will return true (Boolean) if Prime, false if not. Hint: Check every number up to the square root. To do this, try a for loop.

Step Two

Write another function called printPrimes that will print (console log) all the Primes up to an arbitrary limit. For example, if you invoke your function with printPrimes(97), it will print all the Prime numbers up to and including 97. This function can call on the previous checkPrime function.

Insert Dash

Write a function insertDash that accepts a number as a parameter and returns a string with a dash inserted between any consecutive odd numbers. There should not be a dash at the end, it goes only between numbers.

const numWithDashes = insertDash(454793);

console.log(numWithDashes);
=> 4547-9-3
Commit.

Reverse a String

Write a function reverseString that takes a string as a parameter and returns that string with the letters reversed without using .split(), .reverse(), or .join().

Commit.
  1. Make your palindrome function from problem two above work regardless of spacing (or capitalization). So, for example, "Sit on a potato pan Otis" or "Bird rib" would pass the test.

    Commit.

  2. Make your palindrome function work even if the string contains punctuation. So: "Sit on a potato pan, Otis!!!" or "A man, a plan, a canal: Panama." or "Cigar? Toss it in a can! It is so tragic." would pass the test.

    Commit.

  3. Make a "word palindrome" function that returns true if the words in a phrase are the same backwards and forwards. It should not care about spacing, capitalization, or punctuation. For example the following string would pass the test:

    "Son, I am able," she said. "Though you scare me, watch!" said I, "Beloved," I said, "watch me scare you!" Though, said she: "able am I, son."

    Commit.

  4. You still want more?!?! Do you even sleep? Create an account on Project euler and keep working on those problems.

More Practice

For more practice with functions, check out the website, Codewars. Codewars is a code challenge platform with hundreds of challenges of different difficulty levels. Create an account, start on the easiest difficulty level, and slowly work your way up.

functions-scope-hw's People

Contributors

reubenayres avatar reuben-ga avatar msolorio avatar jimbojones1 avatar h64 avatar meganroberta80 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.