【发布时间】:2019-03-21 18:04:58
【问题描述】:
我有这个练习,我需要帮助来了解我哪里出错了。到目前为止,这是我的代码。
// Exercise Two: In this exercise you will be given an array called 'cents'
// This array is a list of prices, but everything is in cents instead of dollars.
// Using the map method, divide every value by 100 and save it as a new array 'dollars'
function exerciseTwo(cents){
function mapMethod(array, cb) { // created the map method
let dollars = []; // declaring the new array 'dollars'
for (i=0; i < array.length; i++) { //iterating through the loop
let updatedValue = cb(array[i] / 100); // dividing the iteration by 100
dollars.push(updatedValue); //pushing the updated value to the new array 'dollars'
}
return dollars;
}
// Please write your answer in the lines above.
return dollars; // getting error that 'dollars' is not defined :(
}
【问题讨论】:
-
你应该使用原生js
map()method. -
欢迎来到 Stack Overflow!请拿起tour,环顾四周,通读help center,尤其是How do I ask a good question? 分配通常不是任意的;您的讲师、教程或课程将涵盖使您能够做到这一点的必要主题。 查看您的课程资料、课堂笔记等,并尝试一下。 如果您遇到特定问题,请彻底研究, search thoroughly,如果您仍然遇到问题,请发布您的代码和具体问题的描述。人们会很乐意提供帮助。
-
你没有在代码中的任何地方调用你的函数
mapMethod,而且美元是在mapMethod范围而不是exerciseTwo范围中定义的
标签: javascript arrays methods callback