JS篇 - Array Map() Exercises

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

  1. Capitalize the string;
  2. 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' ]
  1. 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
    }
]));
  1. 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>')
  })
}
### 关于面包板电源模块 MB102 的 USB 供电规格及兼容性 #### 1. **MB102 基本功能** 面包板电源模块 MB102 是一种常见的实验工具,主要用于为基于面包板的小型电子项目提供稳定的电压输出。它通常具有两路独立的稳压输出:一路为 5V 和另一路可调电压(一般范围为 3V 至 12V)。这种设计使得它可以满足多种芯片和传感器的不同工作电压需求。 #### 2. **USB 供电方式** MB102 支持通过 USB 接口供电,输入电压通常是标准的 5V DC[^1]。由于其内部集成了 LM7805 稳压器以及可调节电位器控制的直流-直流变换电路,因此即使输入来自电脑或其他低功率 USB 设备,也能稳定地向负载供应电力。不过需要注意的是,如果项目的功耗较高,则可能超出某些 USB 端口的最大电流能力(一般是 500mA),从而引起不稳定现象或者保护机制启动断开连接的情况发生。 #### 3. **兼容性分析** 该型号广泛适用于各种微控制器单元 (MCU),特别是那些像 Wemos D1 R32 这样可以通过杜邦线轻松接入并共享相同逻辑级别的系统[^2]。另外,在提到 Arduino Uno 板时也表明了良好的互操作性,因为两者均采用相似的标准接口定义与电气特性参数设置[^4]: - 对于需要 3.3V 工作环境下的组件来说,只需调整好对应跳线帽位置即可实现精准匹配; - 当涉及到更多外围扩展应用场合下,例如带有多重模拟信号采集任务的情形里,利用 MB102 提供干净无干扰的基础能源供给就显得尤为重要了[^3]。 综上所述,对于打算构建以单片机为核心的原型验证平台而言,选用具备良好声誉记录且易于获取配件支持服务链路上下游资源丰富的品牌产品——如这里讨论过的这款特定类型的配电装置不失为明智之举之一。 ```python # 示例 Python 代码展示如何检测硬件状态 import machine pin = machine.Pin(2, machine.Pin.IN) if pin.value() == 1: print("Power supply is stable.") else: print("Check your connections and power source.") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值