0

new to Javascript and trying to make a little thing that will let me decide what movie I'm going to watch. I've made a test list of some movies. Then I'm shown two movies at a time from the list, and each time I have to veto one of the movies. This should continue until there's only one movie left, at which point a pop-up will tell me what movie to watch.

The problem is, it's not working properly. It doesn't seem to delete the last item on my list no matter what I do, and sometimes movies do not delete at all. Here is the code I'm using. Could someone help point me in the right direction?

var options = [ "ET", "Schindler’s List", "Up", "What’s Eating Gilbert Grape", ]; var process = function() { while (options.length > 1) { for (i = options.length-1; i >= 1; i--) { var select = prompt("VETO one of the following: 1. " + options[i] + " 2. " + options[i-1]); if (select === 1) { options.splice(i, 1); } else { options.splice(i-1, 1); } } } }; process(); alert(options); 
3
  • 1
    Usually, modifying an array while iterating in it is a bad idea. How do you want to select the different movies? Why not using a random? Commented Jan 27, 2014 at 5:25
  • So ncabral's answer solved my question, but if there's a better way to do this, I'm all ears. I'm still very much a beginner. Basically I want all the movies on my list to be presented to the user, two at a time. The user will then veto one of the two movies, and be presented with two more movies, etc, until there is only one movie remaining. This is basically how me and my gf choose what movie we're going to watch, so I just wanted to automate it some. In an ideal world, it would compare the movies randomly, but I didn't know how to do that so I did the array. Commented Jan 27, 2014 at 6:13
  • No problem. Cool usage of javascript to please your girlfriend :) Commented Jan 27, 2014 at 18:14

2 Answers 2

2

The select variable returns as a string. Hence,

select === 1 // always false select === '1' // works as expected 

Modified Source:

var options = [ "ET", "Schindler’s List", "Up", "What’s Eating Gilbert Grape", ]; var process = function() { while (options.length > 1) { for (var i = options.length-1; i >= 1; i--) { var select = prompt("VETO one of the following: 1. " + options[i] + " 2. " + options[i-1]); if (select === '1') { // changed options.splice(i, 1); } else { options.splice(i-1, 1); } } } }; process(); alert(options); 

Also, use var to declare variables - always.

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

2 Comments

Thank you so much! Can't believe I made such a silly mistake. I also appreciate the tip re: var.
You're welcome. It's always good to 'use strict'; in your js file and use something like JSHint to avoid potential problems.
0

if( select === 1) always false because select will return as string.... instead of select === 1 use select === "1" or select == 1

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.