-2

I need to find the max value (b) of the array then take that value convert it then put everything into another function and display it like so: "Lake Baikal:1640.43 meters."

function lakeDepth() {
  let lakeData = {
    "Caspian Sea": 560,
    "Tarn Hows": 53,
    "Crater Lake": 324,
    "Lake Tanganyika": 803,
    "Lake Vostok": 546,
    "Lake Baikal": 897,
  };
  result = Object
    .keys(lakeData)
    .sort(function(a, b) {
      return lakeData[b] - lakeData[a];
    })
    .map(Number);

  console.log(result);

}
lakeDepth();

function fathomsToMeter() {
  let deepestInMeter = result * 1.8288;
  return deepestInMeter;

}

function displayData() {
  console.log(result + deepestInMeter + "meter");


}

how do i get just the "b" part of the array do the calculation in the function and put it together at the end? thanks in advance for any help.

giuseppedeponte
  • 2,366
  • 1
  • 9
  • 19
steakman
  • 33
  • 4
  • You don't have an ***array*** here. You have an ***object***. Additionally, you have a syntax error with your object - - you need to remove the final comma after the last key/value pair in the object. – Scott Marcus Nov 10 '19 at 16:54

2 Answers2

0

You can easily find deepest lake as below, regarding to your code;

    let lakeData = {
            "Caspian Sea": 560,
            "Tarn Hows": 53,
            "Crater Lake": 324,
            "Lake Tanganyika": 803,
            "Lake Vostok": 546,
            "Lake Baikal": 897,
        };
    
    let _deepest = 0
    let _key = ""

    Object.keys(lakeData).forEach(function(key) {
      if (lakeData[key] > _deepest)
      {
        _deepest = lakeData[key]
        _key = key
      }
    });
    
    console.log(lakeData[_key], _key)

Loop through all keys in object, follow their values and compare in every iteration, and find the biggest one.

Zubeyir
  • 11
  • 6
  • Answers should describe what the original problem was and how your code solves the problem. Not just include working code. – Scott Marcus Nov 10 '19 at 16:57
0

Your code gives an array of lake names sorted by depth, so all that was needed was to select the first from that array using [0], then select the actual depth using lakeData[nameOfLake] which looks into the lakeData object and returns the value of the key nameOfLake.

Let me know if you have any questions.

const lakeData = {
  "Caspian Sea": 560,
  "Tarn Hows": 53,
  "Crater Lake": 324,
  "Lake Tanganyika": 803,
  "Lake Vostok": 546,
  "Lake Baikal": 897
};

const lake = Object.keys(lakeData).sort((a, b) => lakeData[b] - lakeData[a])[0];

console.log(`${lake}: ${lakeData[lake] * 1.8288} metres`);

Following your comment of 11/11:
An example of a template literal with a new line:

console.log(`The Deepest lake is: ${lake}
This lake is ${lakeData[lake] * 1.8288}m deep.`)
Frazer
  • 1,049
  • 1
  • 10
  • 16
  • 1
    Answers should describe what the original problem was and how your code solves the problem. Not just include working code. – Scott Marcus Nov 10 '19 at 16:57
  • @ScottMarcus Fair point, hopefully my edit is useful to steakman. – Frazer Nov 10 '19 at 17:03
  • *Hopefully this answer also shows how code can be made simpler.* is not an explanation of what your code does or how it works. – Scott Marcus Nov 10 '19 at 17:06
  • Thank you Frazer, this is working and is short, but how would i console.log it in another function? as i cant seem to connect the lakedata if i consolel.og it in another function, also how can i edit the jquery part? i havent used it before i need to add the .toFixed(2), thank you in advance – steakman Nov 11 '19 at 09:58
  • so i got this console.log, i just need to add the .toFixed(2) and need to present it in another function how would i go about doiung this? console.log("The Deepest lake is: " + `${lake}` + "\n" + "This lake is " + `${lakeData[lake] * 1.8288}` + "m deep.") – steakman Nov 11 '19 at 10:16
  • @steakman the ${x} bits aren't jquery but template literals. You use a backtick ` instead of sing or double quotes; within those backticks the pattern ${abc} will match a variable, in this case the variable abc. It's an alternative to building a string via +=. As an added bonus you don't need a \n for a new line, just put your output on another line within the literal. I'll add to my answer for an example. – Frazer Nov 11 '19 at 10:54
  • @Frazer ok perfect thank you – steakman Nov 11 '19 at 12:37
  • @steakman great, glad to have helped. If you find an answer helpful you can upvote it and/or mark it as the accepted answer. This encourages people to contribute. – Frazer Nov 11 '19 at 14:35
  • 1
    @Frazer i upvoted it but it only shows after 15 reputation, so it says atleast. – steakman Nov 12 '19 at 18:25
  • Thank you, and well done for getting your reputation points. – Frazer Nov 22 '19 at 13:06