Today, I am going to use the built-in .map() method on arrays to solve all of these problems
These assignments are from https://coursework.vschool.io/array-map-exercises/.
map() method documents: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
1) Make an array of numbers that are doubles of the first array.
function doubleNumbers(arr){
// your code here
}
console.log(doubleNumbers([2, 5, 100])); // [4, 10, 200]
Answer:
function doubleNumbers(arr){
let doubleArr = arr.map(x=>x*2);
return doubleArr;
}
console.log(doubleNumbers([2, 5, 100]))
2) Take an array of numbers and make them strings
function stringItUp(arr){
// your code here
}
console.log(stringItUp([2, 5, 100])); // ["2", "5", "100"]
Answer:
function stringItUp(arr){
let strArr = arr.map(String);
return strArr;
}
console.log(stringItUp([2, 5, 100]));
Here is another solution of converting int array to string array.
var arr = [2, 5, 100];
var strArr = arr.map(function(e){return e.toString()});
console.log(strArr); //
However, From my point of view, better to use arr.map(String).
3. Capitalize each of an array of names
function capitalizeNames(arr){
// your code here
}
console.log(capitalizeNames(["john", "JACOB", "jinGleHeimer", "schmidt"]));
// ["John", "Jacob", "Jingleheimer", "Schmidt"]
Answer
function capitalizeNames(arr){
let uppClassArr=arr.map(function(string){
return string.substr(0,1).toUpperCase() + string.substr(1).toLowerCase();
})
return uppClassArr
}
console.log(capitalizeNames(["john", "JACOB", "jinGleHeimer", "schmidt"]));
Notification (idea steps):
Here is a point that string arrays that we have is random, which means the name
- Capitalize the string;
- converting the letter [1, end] to the lowercase.
if you do this:
function capitalizeNames(arr){
let uppClassArr=arr.map(function(string){
return string.toUpperCase()
})
return uppClassArr
}
console.log(capitalizeNames(["john", "JACOB", "jinGleHeimer", "schmidt"]));
Result would like this:
[ 'JOHN', 'JACOB', 'JINGLEHEIMER', 'SCHMIDT' ]
- Make an array of strings of the names
function namesOnly(arr){
// your code here
}
console.log(namesOnly([
{
name: "Angelina Jolie",
age: 80
},
{
name: "Eric Jones",
age: 2
},
{
name: "Paris Hilton",
age: 5
},
{
name: "Kayne West",
age: 16
},
{
name: "Bob Ziroll",
age: 100
}
]));
// ["Angelina Jolie", "Eric Jones", "Paris Hilton", "Kayne West", "Bob Ziroll"]
Answer:
function namesOnly(arr){
let onlyNameArr = arr.map(x=>x.name);
return onlyNameArr;
}
console.log(namesOnly([
{
name: "Angelina Jolie",
age: 80
},
{
name: "Eric Jones",
age: 2
},
{
name: "Paris Hilton",
age: 5
},
{
name: "Kayne West",
age: 16
},
{
name: "Bob Ziroll",
age: 100
}
]));
- Make an array of strings of the names saying whether or not they can go to The Matrix
function makeStrings(arr){
// your code here
}
console.log(makeStrings([
{
name: "Angelina Jolie",
age: 80
},
{
name: "Eric Jones",
age: 2
},
{
name: "Paris Hilton",
age: 5
},
{
name: "Kayne West",
age: 16
},
{
name: "Bob Ziroll",
age: 100
}
]));
// ["Angelina Jolie can go to The Matrix",
// "Eric Jones is under age!!",
// "Paris Hilton is under age!!",
// "Kayne West is under age!!",
// "Bob Ziroll can go to The Matrix"]
Answer:
function makeStrings(arr){
let reFormArr = arr.map(arr =>{
if(arr.age>=18){
console.log(arr.name + ' can go to The Matrix')
}else {
console.log(arr.name + " is under age!!");
}
})
}
Hints: Using map to reformat objects in an array
reference:
6) Make an array of the names in h1s, and the ages in h2s
function readyToPutInTheDOM(arr){
// your code here
}
console.log(readyToPutInTheDOM([
{
name: "Angelina Jolie",
age: 80
},
{
name: "Eric Jones",
age: 2
},
{
name: "Paris Hilton",
age: 5
},
{
name: "Kayne West",
age: 16
},
{
name: "Bob Ziroll",
age: 100
}
]));
// ["<h1>Angelina Jolie</h1><h2>80</h2>",
// "<h1>Eric Jones</h1><h2>2</h2>",
// "<h1>Paris Hilton</h1><h2>5</h2>",
// "<h1>Kayne West</h1><h2>16</h2>",
// "<h1>Bob Ziroll</h1><h2>100</h2>"]
Answer:
function readyToPutInTheDOM(arr){
let reFormArr = arr.map(arr =>{
console.log('<h1>'+arr.name+'</h1>'+"<h2>"+arr.age+'</h2>')
})
}