1

I'm having problems with splice in my code, I don't know why is not working, I need to remove the minimum integer from the array.

Here's my code:

var players = [
    "Jug 1",
    "Jug 2",
    "Jug 3",
    "Jug 4"
];

var arrTotal = [72, 71, 70, 75];

function winners(arr) {
    var fstPlace = [], sndPlace= [];
    var min = Math.min.apply(null, arr);
    console.log(min);
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == min) {
            fstPlace.push(arr.indexOf(min, i));
        }
    }
    if (fstPlace.length == 1) {
        console.log("1st: " + fstPlace);
        arr.splice(min, 1);
        console.log(arr);
    }
    else {
        console.log("Tie: " + fstPlace);
    }
}

winners(arrTotal);

1 Answer 1

1

You have to give the index of the item to be removed

var players = [
  "Jug 1",
  "Jug 2",
  "Jug 3",
  "Jug 4"
];

var arrTotal = [72, 71, 70, 75];

function winners(arr) {
  var fstPlace = [],
    sndPlace = [];
  var min = Math.min.apply(null, arr);
  snippet.log('min: ' + min);

  for (var i = 0; i < arr.length; i++) {
    if (arr[i] == min) {
      fstPlace.push(arr.indexOf(min, i));
    }
  }
  snippet.log('fstPlace: ' + fstPlace)
  if (fstPlace.length == 1) {
    snippet.log("1st: " + fstPlace);
    arr.splice(fstPlace[0], 1);
    snippet.log('result: ' + arr);
  } else {
    snippet.log("Tie: " + fstPlace);
  }
}

winners(arrTotal);
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

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

2 Comments

I can't use the value itself?
Thank you Arun, it works perfect. I must wait 6 minutes to accept the answer. :)

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.