Skip to content

Instantly share code, notes, and snippets.

@Victor-Villacis
Last active August 3, 2020 14:54
Show Gist options
  • Save Victor-Villacis/e83f9029eebb2efd1db23f0cb9c0e5a8 to your computer and use it in GitHub Desktop.
Save Victor-Villacis/e83f9029eebb2efd1db23f0cb9c0e5a8 to your computer and use it in GitHub Desktop.
.map and parseInt created by victor_villacis - https://repl.it/@victor_villacis/map-and-parseInt
/***note that .split will turn a string of numbers into an array ***/
//let a = '1,2,3,4,5'
//a.split(',')
/**********take numbers in an array and convert to string.**********/
var numbers = [1,2,3,4,5,6]
let toString = numbers.map(String);
console.log(toString)
/*********take numbers in a string and convert into an array*******/
//var numbers = ['1','2','3','4','5']
// let parseInteger = function(a){
// return parseInt(a);
// }
// let x = numbers.map(parseInteger)
// console.log(x)
/*********more consise ways******/
// ["1","2","3"].map(function(value) {return parseInt(value)})
// ['1', '2', '3'].map( str => parseInt(str) );
// ['1', '2', '3'].map(Number)
/*********.Map and parseInt explained*******/
/****map has 3 parmeters: value of element, index of element, array to be passed - (object traversed)****/
// var array = [1,2,3];
// var myMapFunction = function(a,b,arrayPassed){
// //runs 3 times since there are 3 elemnts to loop over
// console.log('Element a is ' + a + ' at index ' + b + ' at array ' + arrayPassed)
// return arrayPassed
// }
// array.map(myMapFunction);
/****parseInt has 2 parameters: string to be passed, redix****/
// array = [1,2,3];
//array.map(parseInt)
//therefore becuase of .map, which has 3 arguments it is returing
//parseInt("1", 0, theArray)
//parseInt("2", 1, theArray)
//parseInt("3", 2, theArray)
//where theArray is the original array ["1","2","3"].
//thus it ignores the 3rd parameter, but not the second and the call returns [1,Nan,Nan]
/**Example of squaring, or multiplying by itself each number in an array ***/
// const array = [1,2,3,4,5,6];
// const square = x => Math.pow(x, 2);
// const squares = array.map(square);
// console.log(`Original array: ${array}`);
// console.log(`Squared array: ${squares}`);
/***or***/
// let add = function(a){
// return a * a * a * a ;
// }
@Victor-Villacis
Copy link
Author

Examples of using .map and parseint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment