Project Make a Library Part 2 #1 | Parameters, Return, and Libraries ('23-'24) - Code.org
Mon Mar 18 2024 22:51:51 GMT+0000 (Coordinated Universal Time)
Saved by
@piercethedeamon
//Outputs a number within the range of indexes
//return {integer} - index within the cereal data
function getRandomIndex() {
var nameList = getColumn("Cereal Nutrition", "Name");
var index = randomNumber(0, nameList.length - 1);
}
//returns name at that index
// index {integer} - location in list
// return {string} - name at that index
function getName(index) {
var nameList = getColumn("Cereal Nutrition", "Name");
var name = nameList [index];
return name;
}
// Outputs the location of a image of the cereal box
// index {integer} - number of index of image you need
// picture {string} - image a
function getCerealImage(index) {
var allPictures = getColumn("Cereal Nutrition", "Box Image");
var picture = allPictures [index];
return picture;
}
//Check Calories, Check True
function checkCalories(guess, index) {
var correct = false;
var allCalories = getColumn("Cereal Nutrition", "Calories");
if (guess == allCalories [index]) {
correct = true;
}
return correct;
}
// Compares calories guessed to cereal and counts matches
// guess {intger} - user input the amount calories
// count {integer} - number of other cereal with that many calories
function allCaloriesCheck(guess) {
var allCalories = getColumn("Cereal Nutrition", "Calories");
var count = 0;
for (var i = 0; i < allCalories.length; i++) {
if (guess == allCalories[i]) {
count = count + 1;
}
}
return count;
}
//Check Sugar, Check True
function checkSugar(guess, index) {
var correct = false;
var allSugar = getColumn("Cereal Nutrition", "Sugar");
if (guess == allSugar [index]) {
correct = true;
}
return correct;
}
//Comapres sugar guessed to cereal and counts matches
//guess (integer) - nurser input the amount of Sugar
//count (integer) - number of other cereal with that many Sugar
function allSugarCheck(guess) {
var allSugar = getColumn("Cereal Nutrition", "Sugar");
var count = 0;
for (var i = 0; i < allSugar.length; i++) {
if (guess == allSugar[i]) {
count = count + 1;
}
}
return count;
content_copyCOPY
https://studio.code.org/s/csp7-2023/lessons/9/levels/1
Comments