简体   繁体   English

给定数组,写一个 function 找到最大值并返回匹配的字符串

[英]Given array, write a function that finds the max and return the matching string

I have been given an array and need to write a function mostExpensiveItemName(items) and have it return the largest number in the array and return the items name.我得到了一个数组,需要编写一个 function mostExpensiveItemName(items) 并让它返回数组中的最大数字并返回项目名称。

Example output:示例 output:

mostExpensiveItemName(items) //=> "Creation 3005"

Given array:给定数组:

let items = [
  {
    itemName: "Effective Programming Habits",
    type: "book",
    price: 13.99
  },
  {
    itemName: "Creation 3005",
    type: "computer",
    price: 299.99
  },
  {
    itemName: "Finding Your Center",
    type: "book",
    price: 15.00
  }
]

What I have:我有的:

function mostExpensiveItemName(items) {
  if (items.length === 0) return undefined;
  let mostExpensive = items.reduce((total, items) => items.price > total.price ? items : total);

  return mostExpensive.itemName;
}

My solution works but I was wondering if there was a super elementary way of writing it.我的解决方案有效,但我想知道是否有一种超级基本的编写方式。 With a for loop or max() method使用 for 循环或 max() 方法

I have been given an array and need to write a function mostExpensiveItemName(items) and have it return the largest number in the array and return the items name.我得到了一个数组,需要编写一个函数 mostExpensiveItemName(items) 并让它返回数组中的最大数字并返回项目名称。

Example output:示例输出:

mostExpensiveItemName(items) //=> "Creation 3005"

Given array:给定数组:

let items = [
  {
    itemName: "Effective Programming Habits",
    type: "book",
    price: 13.99
  },
  {
    itemName: "Creation 3005",
    type: "computer",
    price: 299.99
  },
  {
    itemName: "Finding Your Center",
    type: "book",
    price: 15.00
  }
]

What I have:是)我有的:

function mostExpensiveItemName(items) {
  if (items.length === 0) return undefined;
  let mostExpensive = items.reduce((total, items) => items.price > total.price ? items : total);

  return mostExpensive.itemName;
}

My solution works but I was wondering if there was a super elementary way of writing it.我的解决方案有效,但我想知道是否有一种超级基本的编写方式。 With a for loop or max() method使用 for 循环或 max() 方法

function mostExpensiveItemName(items) {
    let counter = 0;
        //assign name to return later with conditional
    let name;
    for (let i = 0; i < items.length; i++){
        const e = items[i];
            /* conditional holds price value in the counter variable 
            while it cycles through the loop */
        if (e.price > counter){
            counter = e.price;
                //if true, condition also assigns name to e.itemName
            name = e.itemsName;
            }
        }
    return name;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定数组,编写函数以返回匹配项 - Given array, write function to return matching items 给定露营地数组,编写一个返回总和和匹配输入字符串的函数 - Given campgrounds array, write a function that returns a sum and matching input string 给定露营地数组,编写一个返回字符串和匹配数字的函数 - Given campground array, write a function that returns a string and matching number 给定数组,编写一个函数并返回一个布尔值 - Given array, write a function and return a boolean value 给定一个带数字的数组,如何编写一个递归函数,当2个元素加到目标时,它会在数组中找到索引? - Given an array with numbers, how can I write a recursive function that finds indices in the array when 2 elements add up to a target? 从数组中找出两个数字相加为给定数字的函数 - A function that finds two numbers out of an array that add up to the given number 给定一个露营地数组,一种视图类型为字符串,派对人数为数字,返回一个包含匹配站点的露营地编号的数组 - Given a campgrounds array, a type of view as a string, and the party size as a number, return an array with campsite numbers for the matching sites 编写此递归 function 的更好方法,可在数组中找到奇数个数字 - A better way to write this recursive function that finds an odd amount of numbers in an array 如何编写一个函数来查找数组中最大整数的索引? - How to write a function that finds the index of the greatest integer in an array? Javascript-如何编写一个 function 来找到任何一组数字 arguments 中的最大数 - Javascript- How to write a function that finds the max number in any set of numerical arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM