Monday Flashcards
(4 cards)
how would you capitalize or lowercase str?
turn num into a string and confirm the data type.
lastly, turn strNum into a numeric data type with and without decimals
.toUpperCase( ); and .toLowerCase( );
.toString( ) and typeof(value)
Number(strNum) or parseFloat(strNum) + w/o decimals : parseInt(strNum) – these can be useful when working with numeric keys you need to pass into functions as a number
rest parameter
convert word into an array, separating each word into separate elements.
repeat, separating each letter into separate elements.
word.split ( ); ======> empty parentheses gets one solid string, no matter how many words are in the string
// [‘THE HiSTory of mathematiCS’]
word.split (“ “); ======> quotations WITH A SPACE breaks up words into separate elements
//[‘THE’, ‘HiSTory’, ‘of’, ‘mathematiCS’]
word.split (“”); ======> quotations WITHOUT SPACE breaks up words into separate letters
// [‘T’, ‘H’, ‘E’, ‘ ‘, ‘H’, ‘i’, ‘S’, ‘T’, ‘o’, ‘r’, ‘y’, ‘ ‘, ‘o’, ‘f’, ‘ ‘, ‘m’, ‘a’, ‘t’, ‘h’, ‘e’, ‘m’, ‘a’, ‘t’, ‘i’, ‘C’, ‘S’]
how would you sum the values in this array using array method(s)?
const originalArray = [ {price: 10.00}, {price: 5.50}, {price: 30.00} ];
reduce practice questions :
Intermediate Algos (solve using reduce) – https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/#intermediate-algorithm-scripting
https://coursework.vschool.io/array-reduce-exercises/
const sum = originalArray.reduce((sumVal, curVal) => sumVal + curVal.price, 0);
___________________________________________________________
important reduce facts to remember :
- what arguments are potentially passed into the method?
- within the first argument, what can/does each parameter represent?
- how do you get output from using reduce?
two arguments in the reduce method, second is optional : (callbackReducerFunction, optionalInitialValue)
reduce((previousValue, currentValue, currentIndex, array) => { /* … */ }
params :
total / acc / previousValue ===> can be the first element in the array you’re reducing, or an empty array/object thats initialized as reduce’s second argument
currVal ===> the first or second element depending on if the initial value has been defined; is your representative of the elements in the array, always
currIndex (optional) ===> index position of currVal in the array
array (optional) ===> a reference for the array being traversed / iterated over
two returns :
return the statement itself
+
you MUST return the ending accumulator within the body of the callbackReducerFunction for there to be an accumulator to start with when you loop around and call the callbackReducerFunction on the next value + when you’ve finished the loop and want to save the accumulator value