Project - Make A Library

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Name(s)____ Abdelrahman Marzouk_________________ Period ___4th Block___ Date __3/24/2024_________

Project - Make a Library

Project Description
Libraries let you build functions that you and others can use to help build more powerful apps in lots of situations. As
the designer of a library you not only need to know how to program, you also need to think about the many different
kinds of programs other people might build.

For this project you will design and build a library of functions around any topic you want. You will have an opportunity
to exchange feedback with another group about how you might use their library to design an app. Finally you will
answer a few questions about the library you designed.

You will submit


● A link to your library of functions
● This completed project guide

Library Requirements
● Your library must contain two or more functions
● At least one function must include
○ A loop
○ An if-statement
○ One or more parameters
○ Return

Step 1 - Brainstorm
Your library can be about any topic. If you need some ideas try the list below

● Strings
○ Capitalize: Capitalize first letter of each word in a sentence (long string)
○ Trim: Remove spaces from beginning and end of a string
○ Remove Letter: removes a given letter from a string
○ Anything else you'd want to do with a string

● Lists
○ Maximum: Find the maximum value in a list
○ Minimum: Find the minimum value in a list
○ Average: Find the average value in a list
○ Count: Count how many times a given value appears in a list
○ Combine: Join two lists together in one longer, larger list
○ Filter Numbers: Keep numbers in a list greater than or less than a provided value
○ Filter Letters: Keep strings in a list that begin with a given letter
○ Numbered List: Turn a list into a string with each item numbered and appearing on a different line
○ Top 3: Return the three biggest numbers in a list, in sorted order (This can be a little tricky)
○ Unique: Return a list of each unique item that appears in (This can be a little tricky)
○ Sort: Return the list in sorted order (This can be a little tricky)
○ Anything else you'd want to do with a list

● Dataset
○ Choose a dataset and build a function that will help a user access or summarize specific information
within that dataset

● Something else
○ As long as your library does something interesting and valuable

Computer Science Principles 1


Step 2 - Design
After you've brainstormed the focus of your library, fill in the table below with the list of functions you intend to build.

Function Name Description, Parameters, Return Loop, If-statement, Param, Return?


Write the name of Write the comments for this function At least one function must include all 4
each function features listed above

maximum(list) // Takes a list and returns the largest value Yes this function should include all of
that appears in the list these features.
// list {list} - the list of items
// return {number} - the largest number in
the list

// Verifies the guessed country name ● Description, Parameters, Return:


Determines if the guessed country is
against the actual country name
correct and updates UI accordingly.
// guessCountry {String} - the country ● Loop: No.
myFunction name input by the user ● If-statement: Yes (checks if the guess
// No parameters taken directly, uses matches the country).
global scope variables ● Param: No direct parameters (uses global
variables).
// return {void} - does not return a value but ● Return?: No (updates UI elements instead).
updates the 'trueorFalse' text element

// Randomly selects a flag from the array ● Description, Parameters, Return: Displays a
random flag from the array.
and updates the flag image element
● Loop: No.
randomize // No parameters ● If-statement: No.
// return {void} - does not return a value but ● Param: No.
updates the 'countryFlag' image element ● Return?: No (updates UI elements instead).

// Loads country data into the 'Country' and ● Description, Parameters, Return: Populates
the global variables with country names
loadCountries 'flag' arrays from a given data source
and flag URLs from a data source.
// No parameters ● Loop: Yes (to loop over the data source and
// return {void} - populates the 'Country' populate the arrays).
and 'flag' global scope arrays ● If-statement: No.
● Param: No direct parameters (assumes
access to a data source).
● Return?: No (performs an action but does
not return a value).

// Validates if the user's input is a valid ● Description, Parameters, Return: Compares


the user's guessed country name against a
country name
list of valid country names and determines
validateGuess // inputText {String} - the country name if the guess is correct.
input by the user to validate ● Loop: Yes (to iterate through the country
// return {Boolean} - returns true if the input list).
is a valid country name, false otherwise ● If-statement: Yes (to determine if the
guessed country matches any country in
the list).
● Param: Yes (guessCountry - String: the
user's guessed country name).
● Return?: Yes (returns a boolean indicating
whether the guess was correct).

Step 3 - Build
Program your library. Make sure you do the following.

Computer Science Principles 2


● Start by building the function that will include all four required features (param, return, if-statement, loop)
● Debug your code as you write it
● Start testing your code early with sample inputs

Step 4 - Test
Write test cases for each of the functions in your library to make sure they return the expected values. Remember that
you should include.

● Inputs (arguments) that will result in your functions behaving differently or returning different values
● Inputs (arguments) just before, at, and after cut offs if your conditionals. For example if your code includes the
statement (value < 2) then try inputs where value is 1, 2, and 3 to see if the algorithm always behaves as you
expect.

Note: Once you are done testing your functions, make sure to comment out any tests you have written into your code
to test your library functions before sending your library to a classmate in Step 5.

Computer Science Principles 3


Step 5 - Share and Feedback
Send your library to a classmate and have them send it to you. You shouldn't need to explain anything about how your
library works or what it is for. The documentation should be good enough.

Your reviewer should fill in the information below.

Reviewer Name: ______Ahmed Belal_____________

Yes Kind Of No

Clear: I can easily understand the overall purpose of Yes


the library and each function within it.

Error-Free: Each function in the library works as Yes


expected.

Useful: I can think of situations where I would want Kind Of


to use this library

I like: Give feedback on at least one thing you like in the library

I appreciate the interactive aspect of the library that allows users to engage with the learning material through
guessing. The simple and clear structure of the functions (myFunction and randomize) makes the code easy to
understand and follow. It encourages user participation and can be a very effective way of learning about
countries and their flags.

I wish: Give one problem or limitation of the library

One limitation is that the code relies on exact matches for the country names, which could be a problem if
users make minor spelling errors or if there are variations in country names (such as "USA" versus "United
States"). This could lead to unnecessary frustration for users.

What if: Give one idea for how to improve the library

To improve the library, what if we implemented a more forgiving input validation? We could include features
such as autocorrect suggestions or a fuzzy search mechanism that can handle minor spelling errors or
different valid names for the same country. Additionally, adding a hint feature that provides partial
information about the country when the user struggles to guess the correct answer could enhance the learning
experience.

Step 6 - Improve
Based on the feedback above, make final improvements to your library.

BEFORE IMPROVEMENTS CODE:


//This are the variables that contain the important information/data for this app
var Country = getColumn("Countries and Territories", "Country Name");
var flag = getColumn("Countries and Territories", "Flag");

//If you have made a guess, this will state if you guess was right or wrong
onEvent("guessButton", "click", function( ) {
myFunction();
});

Computer Science Principles 4


//This function declared if the guess that was made it true or false
function myFunction() {
var guessCountry = getProperty("guessText", "text");
if (guessCountry == Country) {
setProperty("trueorFalse", "text", "True!!");
} else {
setProperty("trueorFalse", "text", "False!!");
}
}

//If the button is pressed, it will switch the flags to another country
onEvent("randomButton", "click", function( ) {
randomize();
});

//This will randomize the country flags that will be shown on the app
function randomize() {
var index = randomNumber(0, flag.length - 1);
setProperty("countryFlag", "image", flag[index]);
}

After IMPROVEMENTS CODE:


var Country = getColumn("Countries and Territories", "Country Name");
var flag = getColumn("Countries and Territories", "Flag");

// When a guess is made, check if it is right or wrong


onEvent("guessButton", "click", function() {
var guessCountry = getProperty("guessText", "text");
if (validateGuess(guessCountry)) {
setProperty("trueorFalse", "text", "True!!");
} else {
setProperty("trueorFalse", "text", "False!!");
suggestCountries(guessCountry);
}
});

// Function to validate the user's guess using fuzzy matching


function validateGuess(guessCountry) {
return fuzzyMatch(guessCountry, Country);
}

// Function for fuzzy matching input with country list


function fuzzyMatch(input, listOfCountries) {
input = input.toLowerCase();
for (var i = 0; i < listOfCountries.length; i++) {
if (listOfCountries[i].toLowerCase().includes(input)) {
return true;
}
}
return false;
}

// Suggest closest country names if the guess is incorrect


function suggestCountries(guessCountry) {
var suggestions = getClosestMatches(guessCountry, Country);
setProperty("suggestionsText", "text", "Did you mean: " + suggestions.join(", ") + "?");

Computer Science Principles 5


}

// Function to find the closest matches to the user's guess


function getClosestMatches(input, listOfCountries) {
// Placeholder for an actual closest match finding algorithm
// For now, just return an empty array
return []; // Replace with actual suggestions
}

// Randomize the country flags when the button is pressed


onEvent("randomButton", "click", function() {
randomizeFlag();
});

// Function to randomize the country flags shown in the app


function randomizeFlag() {
var index = randomNumber(0, flag.length - 1);
setProperty("countryFlag", "image", flag[index]);
}

// Provide a hint when the hint button is pressed


onEvent("hintButton", "click", function() {
provideHint();
});

// Function to generate and display a hint for the current country


function provideHint() {
var currentFlagIndex = Country.indexOf(getProperty("countryFlag", "image"));
var hint = generateHintForCountry(Country[currentFlagIndex]);
setProperty("hintText", "text", "Hint: " + hint);
}

// Generate a hint for the country


function generateHintForCountry(country) {
// Implement hint generation logic
// This is an example hint, it should be replaced with real hint logic
return "It's in Europe and starts with 'F'."; // Example hint
}

Step 7 - Acknowledge Collaborators / Sources


It is important to acknowledge any code you write that was developed collaboratively or that you received from another
source. In the table below list work you completed with a collaborative partner or got from another source.

Name / Source Lines of Code / Portions of Program Contributed

ChatGPT // Validates the user's guessed country name with fuzzy matching
function validateGuess(guessCountry) {
// Implement fuzzy matching logic here
// This could be a simple case-insensitive match or use a more advanced library like
Fuse.js
var isMatch = fuzzyMatch(guessCountry, Country);
return isMatch;
}

function fuzzyMatch(input, listOfCountries) {

Computer Science Principles 6


// Pseudo-code for fuzzy matching
input = input.toLowerCase();
for (var i = 0; i < listOfCountries.length; i++) {
if (listOfCountries[i].toLowerCase().includes(input)) {
return true;
}
}
return false;
}

Computer Science Principles 7


Step 8 - Free Response

1. Copy and paste a function definition from your library that:


a. uses one or more parameters that have an effect on the functionality of the procedure; and
b. implement an algorithm that includes sequencing, selection and iteration.

// Function to find the closest matches to the user's guess


function getClosestMatches(input, listOfCountries) {
var suggestions = []; // Holds the close matches
input = input.toLowerCase(); // Sequence: Prepare the input for comparison

// Iteration: Go through each country in the provided list


for (var i = 0; i < listOfCountries.length; i++) {
// Selection: Check if the country starts with the same letter as the input
if (listOfCountries[i].toLowerCase().startsWith(input[0])) {
suggestions.push(listOfCountries[i]); // Add it to suggestions if it matches
// Sequence: Limit the suggestions to avoid overwhelming the user
if (suggestions.length >= 3) break; // Another selection, for performance and usability
}
}

return suggestions; // Return the found suggestions


}

2. Provide a written response that:


a. Explains in detailed steps how the algorithm implemented in the identified procedure works. Your
explanation must be detailed enough for someone else to recreate it.

The `getClosestMatches` function suggests country names similar to a user's guess by:

1. Preparing the Input: Converts the user's guess to lowercase for case-insensitive comparison.
2. Iterating Countries: Loops through a list of countries, comparing each to the user's input.
3. Selecting Matches: Identifies countries starting with the same letter as the user's input.
4. Collecting Suggestions: Adds up to three matching countries to a suggestions list.
5. Returning Suggestions: Outputs the list of suggested countries for the user.

This process involves case normalization, iteration through a list, conditional selection of matches, and managing a
collection of suggestions to be returned.

3. Provide a written response that:


a. Describes two calls to the selected function. Each call must pass different arguments that cause a
different segment of code in the algorithm to execute; and
b. Describes what condition(s) is being tested by each call to the procedure; and
c. identifies the result of each call.

Call 1 Call 2

Computer Science Principles 8


Arguments ● input: "C"
What arguments are ● listOfCountries: ["China",
passed? ● input: "X"
(inputs)
"Canada", "Cuba", "Chile",
● listOfCountries: ["China", "Cameroon", "Mexico"]
"Canada", "Xylophone Land",
"Mexico", "Xanadu"]

Conditions Checked ● The function tests if there ● The function checks for
What conditional are any country names that country names starting with
statement is checked to
make different segments
start with the rare letter "X". the common letter "C" and
of the function run tests the mechanism for
Segment of Code Executed: limiting suggestions.

● The loop iterates through Segment of Code Executed:


each country name, checking
if it starts with "X". ● As it finds matches, the
function adds them to the
suggestions array. The code
segment that limits
suggestions to three is
executed once three
matches are found.

Results Since "Xylophone Land" and ● The result will be the first
What will happen / be "Xanadu" both start with "X", they three countries starting with
returned by this function
call?
are added to the suggestions array. "C" from the list, which are
(outputs) However, the function limits the ["China", "Canada",
suggestions to three, which in this "Cuba"]. This demonstrates
case, is not reached, so all how the function caps
matching entries are returned. The suggestions to avoid
result is ["Xylophone Land", overwhelming the user, even
"Xanadu"]. though there are more
matching entries.

Scoring Guidelines

Written Responses (individual) - In the Written Response the student...

Computer Science Principles 9


Responses Point

Selected procedure includes a parameter that impacts the functionality of the procedure /1

Selected procedure includes sequencing, selection, and iteration /1

Response 2 explains how the selected procedure accomplishes its task /1

Response 3 Call 1: the response indicates the arguments that are passed, the specific condition that is /1
checked, and the result of the call.

Response 3 Call 2: the response indicates the arguments that are passed, the specific condition that is /1
checked, and the result of the call.

Program Code

Library includes two or more functions that accomplish related tasks /2

All functions include comments that explain the purpose of the function and the parameters /2

All functions include tests that demonstrate the code working as expected /2

Library code runs without errors /1

Project Guide

Project guide is complete /1

Student provided clear and actionable feedback to a classmate on their project guide /1

Total /14

Computer Science Principles 10

You might also like