0

I am trying to assign the element with the lowest count into a new array property lowestAlph and give me the below iteration where -2 is being stored, instead of simply outputting the lowest count -2 on its own :

[["n"], ["o", "p"], ["q"], ["r", "s"], -2]

However, it's not printing the correct output with the push method. Could someone please help?

const example = {
    alph: [
        [["a"], ["b", "c"], ["d"], ["e", "f"], 23],
        [["g"], ["h", "j"], ["k"], ["l", "m"], 19],
        [["n"], ["o", "p"], ["q"], ["r", "s"], -2],
        [["t"], ["u", "w"], ["x"], ["y", "z"], 25]
    ],

    lowestAlph: [],


    calcLow: function () {
        let lowest = this.alph[0][4];
        for (let i = 0; i < this.alph.length; i++) {
            for (let n = i + 1; n < this.alph.length; n++) {
                if (this.alph[n][4] < lowest)
                    lowest = this.alph[n][4]
            }
                //this.lowestAlph.push(this.alph[i])
        }
        return lowest;
    }
}

console.log(example.calcLow());

4 Answers 4

2

It will return expected output if correct below:

  1. remove nested loop (one for loop can find out the lowest value, because your purpose is not sorting the alph)

  2. if found lower value, uses = operator instead of Array.push because it will keep adding new value into your lowestAlph if lower value if found

  3. return this.lowestAlph instead of lowest

const example = {
    alph: [
        [["a"], ["b", "c"], ["d"], ["e", "f"], 23],
        [["g"], ["h", "j"], ["k"], ["l", "m"], 19],
        [["n"], ["o", "p"], ["q"], ["r", "s"], -2],
        [["t"], ["u", "w"], ["x"], ["y", "z"], 25]
    ],

    lowestAlph: [],


    calcLow: function () {
        let lowest = this.alph[0][4];
        for (let i = 0; i < this.alph.length; i++) {
            // for (let n = i + 1; n < this.alph.length; n++) { // remove this for-loop
                if (this.alph[i][4] < lowest)
                    this.lowestAlph = this.alph[i] // assign with the array instead of Array.push
            // }
                //this.lowestAlph.push(this.alph[i])
        }
        return this.lowestAlph; // return this.lowestAlph instead of lowest
    }
}

console.log(example.calcLow());

Sign up to request clarification or add additional context in comments.

Comments

1

You're currently only retrieving the last element in the array this.alph[n][4] and assigning that to the lowest variable.

Instead you should assign the whole array to lowest using this.alph[n] and then retrieve the last element when doing the comparison.

See example below.

const example = {
    alph: [
        [["a"], ["b", "c"], ["d"], ["e", "f"], 23],
        [["g"], ["h", "j"], ["k"], ["l", "m"], 19],
        [["n"], ["o", "p"], ["q"], ["r", "s"], -2],
        [["t"], ["u", "w"], ["x"], ["y", "z"], 25]
    ],

    lowestAlph: [],


    calcLow: function () {
        let lowest = this.alph[0];
        for (let i = 0; i < this.alph.length; i++) {
            for (let n = i + 1; n < this.alph.length; n++) {
                if (this.alph[n][4] < lowest[4])
                    lowest = this.alph[n]
            }
        }
        return lowest;
    }
}

console.log(example.calcLow());

Comments

1

A simple solution would be to clone and sort the array by the 4th index values and return the lowest. For example

const alpha = [
  [["a"], ["b", "c"], ["d"], ["e", "f"], 23],
  [["g"], ["h", "j"], ["k"], ["l", "m"], 19],
  [["n"], ["o", "p"], ["q"], ["r", "s"], -2],
  [["t"], ["u", "w"], ["x"], ["y", "z"], 25]
]

const sorted = [...alpha].sort((a, b) => b[4] - a[4])
console.log(JSON.stringify(sorted.pop()))

Comments

-1

You can use the spread operator

const example = {
    alph: [
        [["a"], ["b", "c"], ["d"], ["e", "f"], 23],
        [["g"], ["h", "j"], ["k"], ["l", "m"], 19],
        [["n"], ["o", "p"], ["q"], ["r", "s"], -2],
        [["t"], ["u", "w"], ["x"], ["y", "z"], 25]
    ],

    lowestAlph: [],


    calcLow: function () {
        let lowest = this.alph[0][4];
        for (let i = 0; i < this.alph.length; i++) {
            for (let n = i + 1; n < this.alph.length; n++) {
                if (this.alph[n][4] < lowest)
                    lowest = this.alph[n][4]
            }
           this.alph[i].includes(lowest) ?
                this.lowestAlph = [...this.alph[i]] : null
        }
        return this.lowestAlph
    }
    
}

console.log(example.calcLow());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.