Array New Array Student1 ---> Jong Student2 ---> Asha Student3 ---> Chin Student4 ---> Kofi Student5 ---> Chee
function mapElements(elements, mapFct) Let newElements = new dynamic array Loop over all elements in "newElements" using loop var "nextElem" Let newElem = mapFct(nextElem) Add nextElem to newElements Return newElements
/** Returns new collection where mapFct is used to generate new elements from passed (param) elements */ function mapElements(elements, mapFct) { let newElements = Array.newDynamic(); for (let nextElem of elements) { let newElem = mapFct(nextElem); newElements.add(newElem); } return newElements }
var nums, squareFct, cubeFct, squares, cubes; nums = [1, 2, 3, 4]; println("Nums: " + nums);1squareFct = (n) => n * n; cubeFct = (n) => n * n * n; squares =2mapElements(nums, squareFct); cubes = mapElements(nums, cubeFct); println("Squares: " + squares); println("Cubes: " + cubes); _hiddenSource_ function mapElements(elements, mapFct) { let newElements = Array.newDynamic(); for (let nextElem of elements) { let newElem = mapFct(nextElem); newElements.add(newElem); } return newElements } //Try It var nums, squareFct, cubeFct, squares, cubes; nums = [1, 2, 3, 4]; squareFct = (n) => n * n; squares = mapElements(nums, squareFct); cubeFct = (n) => n * n * n; cubes = mapElements(nums, cubeFct); println("Nums: " + nums); println("Squares: " + squares); println("Cubes: " + cubes);
//Try It let hours, hourlyRate, wages, wageFct; hours = [10, 20, 5]; //$15/hr hourlyRate = 15; //$15/hr wageFct = (nextHours) => nextHours * hourlyRate; wages = mapElements(hours, wageFct); println("Hours: " + hours); println("Hourly Rate: " + hourlyRate); println("Wages: " + wages); _hiddenSource_ function mapElements(elements, mapFct) { let newElements = Array.newDynamic(); for (let nextElem of elements) { let newElem = mapFct(nextElem); newElements.add(newElem); } return newElements } //Try It let hoursWorked, hourlyRate, wages, wageFct; hoursWorked = [10, 20, 5]; //$15/hr hourlyRate = 15; wageFct = (hours) => hours * hourlyRate; wages = mapElements(hoursWorked , wageFct); println("Hours: " + hoursWorked); println("Hourly Rate: " + hourlyRate); println("Wages: " + wages);
c = (f - 32) * (5/9)
//Try It let temperaturesC, mapFct, temperaturesF, labelFct; temperaturesF = [-15, 0, 32, 50, 80]; fahrenheightToCelsiusFct = (f) => Math.round((f - 32) * (5/9)); temperaturesC = mapElements(temperaturesF, fahrenheightToCelsiusFct); println("Temperatures (F): " + temperaturesF); println("Temperatures (C): " + temperaturesC); labelFct = (value) => '' + value + ' C'; temperaturesC = mapElements(temperaturesC, labelFct); println("Temperatures (C) Labeled: " + temperaturesC); _hiddenSource_ function mapElements(elements, mapFct) { let newElements = Array.newDynamic(); for (let nextElem of elements) { let newElem = mapFct(nextElem); newElements.add(newElem); } return newElements } //Try It let temperaturesC, fahrenheightToCelsiusFct, temperaturesF, labelFct; temperaturesF = [-15, 0, 32, 50, 80]; fahrenheightToCelsiusFct = (f) => Math.round((f - 32) * (5/9)); temperaturesC = mapElements(temperaturesF, fahrenheightToCelsiusFct); println("Temperatures (F): " + temperaturesF); println("Temperatures (C): " + temperaturesC); labelFct = (value) => '' + value + ' C'; temperaturesC = mapElements(temperaturesC, labelFct); println("Temperatures (C) Labeled: " + temperaturesC);
Array Friend0 (Jong) Friend1 (Asha) Friend2 (Chin) Friend3 (Kofi) Friend4 (Chee)
function findFirst(elements, matchFct) { Let i = 0 Loop while i < elements.size() Let nextElem = elements.get(i) If matchFct(nextElem) outputs true return i return -1
/** Returns the index of the first match, or -1 if no match */ function findFirst(elements, matchFct) { for (let i = 0; i < elements.size(); i++) { let nextElem = elements.get(i); if (matchFct(nextElem)) return i; } return -1; }
var nums, matchFct, matchIndex; nums = [0, 2, 4, 11, 10]; println("Nums: " + nums);1matchFct = (nextNum) => nextNum % 2 == 1; matchIndex =2findFirst(nums, matchFct); println("Match Index (first odd): " + matchIndex); //expected result: 3 matchFct = (nextNum) => nextNum > 100; matchIndex = findFirst(nums, matchFct); println("Match Index (first num > 100): " + matchIndex); //expected result: -1 _hiddenSource_ /** Returns the index of the first match, or -1 if no match */ function findFirst(elements, matchFct) { for (let i = 0; i < elements.size(); i++) { let nextElem = elements.get(i); if (matchFct(nextElem)) return i; } return -1; } //Try It var nums, matchFct, matchIndex; nums = [0, 2, 4, 11, 10]; println("Nums: " + nums); matchFct = (nextNum) => nextNum % 2 == 1; matchIndex = findFirst(nums, matchFct); println("Match Index (first odd): " + matchIndex); //expected result: 3 matchFct = (nextNum) => nextNum > 100; matchIndex = findFirst(nums, matchFct); println("Match Index (first num > 100): " + matchIndex); //expected result: -1