{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "layout: post\n", "title: 1.4 Correcting errors\n", "description: Practice with identifying and correcting code blocks\n", "type: ccc\n", "author: Safin Singh and Rohan Juneja\n", "permalink: /basics/js-debug\n", "hide: True\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Home HTML Data Types DOM JavaScript JS Debugging
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[College Board Big Idea 1](https://apclassroom.collegeboard.org/103/home?unit=1)\n", "\n", "## Identifying and Correcting Errors (Unit 1.4)\n", "\n", "> Become familiar with types of errors and strategies for fixing them\n", "\n", "- Review CollegeBoard videos and take notes on blog\n", "- Complete assigned MCQ questions if applicable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Code Segments\n", "\n", "Practice fixing the following code segments!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Segment 1: Alphabet List\n", "\n", "Intended behavior: create a list of characters from the string contained in the variable `alphabet`\n", "\n", "### Code:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "vscode": { "languageId": "javascript" } }, "outputs": [ { "data": { "application/javascript": "\nvar alphabet = \"abcdefghijklmnopqrstuvwxyz\";\nvar alphabetList = [];\n\nfor (var i = 0; i < alphabet.length; i++) {\n alphabetList.push(alphabet[i]);\n}\n\nconsole.log(alphabetList);\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%js\n", "\n", "var alphabet = \"abcdefghijklmnopqrstuvwxyz\";\n", "var alphabetList = [];\n", "\n", "for (var i = 0; i < alphabet.length; i++) {\n", " alphabetList.push(alphabet[i]);\n", "}\n", "\n", "console.log(alphabetList);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### What I Changed\n", "\n", "I changed...\n", "- Used alphabet.length to determine the number of iterations needed to loop through each character in the alphabet string." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Segment 2: Numbered Alphabet\n", "\n", "Intended behavior: print the number of a given alphabet letter within the alphabet. For example:\n", "```\n", "\"_\" is letter number _ in the alphabet\n", "```\n", "\n", "Where the underscores (_) are replaced with the letter and the position of that letter within the alphabet (e.g. a=1, b=2, etc.)\n", "\n", "### Code:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "vscode": { "languageId": "javascript" } }, "outputs": [ { "data": { "application/javascript": "\nvar alphabet = \"abcdefghijklmnopqrstuvwxyz\";\nvar alphabetList = [];\nfor (var i = 0; i < alphabet.length; i++) {\n alphabetList.push(alphabet[i]);\n}\nconsole.log(alphabetList);\nlet letterNumber = 5;\nfor (var i = 0; i < alphabetList.length; i++) {\n if (i === letterNumber) {\n console.log(alphabetList[i] + \" is letter number \" + (i) + \" in the alphabet\");\n }\n}\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%js\n", "\n", "var alphabet = \"abcdefghijklmnopqrstuvwxyz\";\n", "var alphabetList = [];\n", "for (var i = 0; i < alphabet.length; i++) {\n", " alphabetList.push(alphabet[i]);\n", "}\n", "console.log(alphabetList);\n", "let letterNumber = 5;\n", "for (var i = 0; i < alphabetList.length; i++) {\n", " if (i === letterNumber) {\n", " console.log(alphabetList[i] + \" is letter number \" + (i) + \" in the alphabet\");\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### What I Changed\n", "\n", "I changed...\n", "- copied previous codfe for alphabetlist\n", "- used alphabet list to acess the alphabet letter\n", "- the letter's position in the alphabet was found with \"+ (i)\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Segment 3: Odd Numbers\n", "\n", "Intended behavior: print a list of all the odd numbers below 10\n", "\n", "### Code:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "vscode": { "languageId": "javascript" } }, "outputs": [ { "data": { "application/javascript": "\nlet odds = [];\nlet i = 1; \n\nwhile (i < 10) {\n odds.push(i);\n i += 2; \n}\n\nconsole.log(odds);\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%js\n", "\n", "let odds = [];\n", "let i = 1; \n", "\n", "while (i < 10) {\n", " odds.push(i);\n", " i += 2; \n", "}\n", "\n", "console.log(odds);\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### What I Changed\n", "\n", "I changed the evens to odds and I also changed the i = to i < 10 so that it prints numbers below 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# BELOW NOT EDITED" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The intended outcome is printing a number between 1 and 100 once, if it is a multiple of 2 or 5 \n", "- What values are outputted incorrectly. Why?\n", "- Make changes to get the intended outcome." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "javascript" } }, "outputs": [], "source": [ "%%js\n", "\n", "var numbers = []\n", "var newNumbers = []\n", "var i = 0\n", "\n", "while (i < 100) {\n", " numbers.push(i)\n", " i += 1\n", "}\n", "for (var i of numbers) {\n", " if (numbers[i] % 5 === 0)\n", " newNumbers.push(numbers[i])\n", " if (numbers[i] % 2 === 0)\n", " newNumbers.push(numbers[i])\n", "}\n", "console.log(newNumbers) \n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Challenge\n", "\n", "This code segment is at a very early stage of implementation.\n", "- What are some ways to (user) error proof this code?\n", "- The code should be able to calculate the cost of the meal of the user\n", "\n", "Hint:\n", "- write a “single” test describing an expectation of the program of the program\n", "- test - input burger, expect output of burger price\n", "- run the test, which should fail because the program lacks that feature\n", "- write “just enough” code, the simplest possible, to make the test pass\n", "\n", "Then repeat this process until you get program working like you want it to work." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "javascript" } }, "outputs": [], "source": [ "%%js\n", "\n", "var menu = {\"burger\": 3.99,\n", " \"fries\": 1.99,\n", " \"drink\": 0.99}\n", "var total = 0\n", "\n", "//shows the user the menu and prompts them to select an item\n", "console.log(\"Menu\")\n", "for (var item in menu) {\n", " console.log(item + \" $\" + menu[item].toFixed(2)) //why is toFixed used?\n", "}\n", "//ideally the code should support mutliple items\n", "var item = \"burger\"\n", "\n", "//code should add the price of the menu items selected by the user \n", "console.log(total)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hacks\n", "- Fix the errors in the first three segments in this notebook and say what you changed in the code cell under \"What I Changed\" (Challenge is optional)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.6 64-bit", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.8" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" } } }, "nbformat": 4, "nbformat_minor": 2 }