By Diogo Castro


2019-11-08 14:33:21 8 Comments

I'm using a function to get 2 values/variables. i can return one of them but not both? how do i go about being able to extract both to use elsewhere in the code?

My code is:

let lakes = {
    names: ['Caspian Sea', 'Tarn Hows', 'Crater Lake', 'Lake Tanganyika', 'Lake Vostok', 'Lake Baikal'],
    fathoms: [560, 530, 324, 803, 546, 897]
};

function calculateDepth() {
    var deepestLakeName = lakes.names[0];
    let deepestLake = lakes.fathoms[0];

    if (deepestLake < lakes.fathoms[1]) {
        deepestLakeName = lakes.names[1];
        deepestLake = lakes.fathoms[1];
        console.log('The deepest lake is: ' + deepestLakeName);
        //logged the statements to ensure the if statement was working

    } else if (deepestLake < lakes.fathoms[2]) {
        deepestLakeName = lakes.names[2];
        deepestLake = lakes.fathoms[2];
        //console.log('The deepest lake is: ' + deepestLakeName);

    } else if (lakes.fathoms[3] < deepestLake) {
        //code broke with previous deepestLake > lakes.fathoms system, so had to switch them around to get correct result
        deepestLakeName = lakes.names[3];
        deepestLake = lakes.fathoms[3];
        //console.log('The deepest lake is: ' + deepestLakeName);

    } else if (deepestLake < lakes.fathoms[4]) {
        deepestLakeName = lakes.names[4];
        deepestLake = lakes.fathoms[4];
        //console.log('The deepest lake is: ' + deepestLakeName);

    } else if (deepestLake < lakes.fathoms[5]) {
        deepestLakeName = lakes.names[5];
        deepestLake = lakes.fathoms[5];
        //console.log('The deepest lake is: ' + deepestLakeName);

    } else {
        //console.log('The deepest lake is: ' + lakes.names[0])
    }
    return deepestLake; // i can return this, but i also want to return the deepestLakeName so i can output both later on.

}

Thanks for any help and apologies if a similar thing has been asked elsewhere. i searched, but couldn't find anything with 2 specific values that need returning. i can manage 1, but not more..

1 comments

@Nicolas 2019-11-08 14:36:42

You could returns an object with both property :

let lakes = {
    names: ['Caspian Sea', 'Tarn Hows', 'Crater Lake', 'Lake Tanganyika', 'Lake Vostok', 'Lake Baikal'],
    fathoms: [560, 530, 324, 803, 546, 897]
};

function calculateDepth() {
    var deepestLakeName = lakes.names[0];
    let deepestLake = lakes.fathoms[0];

    if (deepestLake < lakes.fathoms[1]) {
        deepestLakeName = lakes.names[1];
        deepestLake = lakes.fathoms[1];
        console.log('The deepest lake is: ' + deepestLakeName);
        //logged the statements to ensure the if statement was working

    } else if (deepestLake < lakes.fathoms[2]) {
        deepestLakeName = lakes.names[2];
        deepestLake = lakes.fathoms[2];
        //console.log('The deepest lake is: ' + deepestLakeName);

    } else if (lakes.fathoms[3] < deepestLake) {
        //code broke with previous deepestLake > lakes.fathoms system, so had to switch them around to get correct result
        deepestLakeName = lakes.names[3];
        deepestLake = lakes.fathoms[3];
        //console.log('The deepest lake is: ' + deepestLakeName);

    } else if (deepestLake < lakes.fathoms[4]) {
        deepestLakeName = lakes.names[4];
        deepestLake = lakes.fathoms[4];
        //console.log('The deepest lake is: ' + deepestLakeName);

    } else if (deepestLake < lakes.fathoms[5]) {
        deepestLakeName = lakes.names[5];
        deepestLake = lakes.fathoms[5];
        //console.log('The deepest lake is: ' + deepestLakeName);

    } else {
        //console.log('The deepest lake is: ' + lakes.names[0])
    }
    return {
        value: deepestLake,
        name: deepestLakeName 
    }; 

}

let newLakes = calculateDepth();
console.log(newLakes.value, newLakes.name);

@Diogo Castro 2019-11-08 14:55:17

apologies @zero298, i will be clearer in future. Nicolas, that did the trick. thank you very much

Related Questions

Sponsored Content

88 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 6087419 View
  • 7589 Score
  • 88 Answer
  • Tags:   javascript arrays

3 Answered Questions

33 Answered Questions

[SOLVED] For-each over an array in JavaScript?

20 Answered Questions

[SOLVED] Checking if a key exists in a JavaScript object?

  • 2009-07-08 13:21:32
  • Adam Ernst
  • 1793637 View
  • 2728 Score
  • 20 Answer
  • Tags:   javascript object

58 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

27 Answered Questions

[SOLVED] What does "use strict" do in JavaScript, and what is the reasoning behind it?

41 Answered Questions

[SOLVED] How do I remove a property from a JavaScript object?

38 Answered Questions

[SOLVED] var functionName = function() {} vs function functionName() {}

69 Answered Questions

[SOLVED] What is the most efficient way to deep clone an object in JavaScript?

86 Answered Questions

[SOLVED] How do JavaScript closures work?

Sponsored Content