Coder Social home page Coder Social logo

arrays-functions-loops's Introduction

Arrays, Functions, and Loops, Oh My! Homework

Notes: Computer programs normally run from the top of the file to the bottom. Programs would be pretty boring if they always did the same thing, though. We will practice using if/else statements to create dynamic programs that perform different actions depending on different conditions.

We will also practice working with arrays. To work with arrays, programmers usually use loops. We'll work on exercises that will have us going through loops, forward and backward (literally). Arrays and loops are going to be fundamental to your knowledge, no matter what computer language you choose to learn.

Another fundamental part of creating code is writing DRY code (code that doesn't repeat itself). We will create functions that give us code snippets that we can reuse without repeating ourselves.

Exercise

Requirements

  • Follow the instructions provided in the main.js file in the starter-code folder.
  • Open the index.html file and examine the console in the Developer Tools. Look here to see if your code is producing the correct results.
  • Submit 2-5 questions that you would like addressed regarding this homework.

Directions

Part 1

We have an array of people's ages, saved in the variable ages. Write a for-loop that will print each of the elements to the console.

Part 2

Part 3

We're comfortable going through an element from beginning to end, but let's flip this around. With the new younger array you created in the last part, print out the elements so that the last element is printed out first, and the first element is printed out last. * Hint: What should the index for your for loop be initialized to if we want to start at the end of the array? * Hint: What would the conditional for your for loop look like?

Part 4

Now that you've finished the first three parts with a for loop, repeat the same exercises (Part 1, Part 2, and Part 3) using a while loop.

  • Hint: Be careful of infinite loops!

Part 5

  • Create a function, getDayOfWeek, that will tell you what day of the week it is.
    • For example, getDayOfWeek(1); would return "Monday", and getDayOfWeek(5); would return "Friday".
  • An invalid day of the week should return the string, "Please enter a valid day of the week".
    • Hint: Using a "switch" statement might result in cleaner code.
  • Can you implement this function without using a switch statement and only one if?

Bonus - Part 6

All this work is making me hungry. Let's create a new function, addPepperoniToPizza, that accepts an array of pizza ingredients.

  • If the array of ingredients already contains pepperoni, then return the pizza ingredients without doing anything to it.

  • If the array of ingredients does not contain pepperoni, then add pepperoni to the array and return it.

    • For example:
      • addPepperoniToPizza(['onions', 'olives', 'pineapple']) should return ['onions', 'olives', 'pineapple', 'pepperoni']
      • And addPepperoniToPizza(['spinach', 'pepperoni', 'mushrooms']) should return ['spinach', 'pepperoni', 'mushrooms'].

Hint: Take a look at the array.indexOf() method. How can we use this to check if we have pepperoni in our array? (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)

Resources

MDN Reference for Arrays

arrays-functions-loops's People

Contributors

connorjclark avatar buffalocheck avatar

Watchers

 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.