2016-03-20 18 views
0

Ich muss einen Reiseplaner erstellen: Ich habe drei Linien mit mehreren Stopps und alle haben einen gemeinsam. Das habe ich bisher mit Javascript gemacht; Ich weiß nicht, ob es überhaupt Sinn macht.Index des Elementes des verschachtelten Arrays

Was ich zu tun versuchte ist ... meinen Startpunkt und Endpunkt meiner Reise zu definieren, versuche den commonStop zu definieren, also könnte ich als nächstes mit der if/else-Anweisung meinen Teil der Reise definieren. .aber ich blieb stecken ... verursachen seltsame Array-Arrays, ich bin mir nicht sicher über die Syntax, die ich verwendet habe..ist es eine Möglichkeit, diesen Code zu verbessern? macht es lesbarer und weniger komplex?

Jede Hilfe wäre zu schätzen ... und wenn Sie erklären könnten, was Sie tun würden, denn ich bin wirklich neu und irgendwie schon verloren.

line1 = ['Times Square', '34th', '28th', '23rd', 'Union Square', '8th']; 

line2 = ['6th', 'Union Square', '3rd', '1st']; 

line3 = ['Grand Central', '33rd', 'Union Square' , 'Astor Place']; 

var allLines = [line1, line2, line3]; 

console.log(allLines); 

// Ask the user for the start point and start line; end point and endline. 
// var startLine = prompt ('Which line is your start?'); 
// var startPoint = prompt('Where you are getting on at?'); 
// var endPoint = prompt ('Where do you wanna get of at?'); 
// var endLine = prompt('Which line is that?'); 

var startLine = 'line2'; 
var startPoint = '6th'; 
var endPoint = '1st'; 
var endLine = line2; 

// commonstop1 = allLines[0][4]; 
// commonstop2 = allLines[1][1]; 
// commonstop3 = allLines[2][2]; 

//if my startline is equal to my end line, my journey can proceed 
//else I need to stop. 

    var ptJourney = function (startPoint, endPoint) { 

    if (startLine === endLine) { 
     console.log('Your journey goes from ' + startPoint + ' to ' + endPoint + ' without changing line!'); 
    }  

    else { 
      console.log('You need to change line at Union Square!'); 
    };  

function findStart(array, startPoint) { 

    for(var i = 0; i < allLines.length; i++) { 

    for (var j=0; j < allLines.length[i]; j++){ 

     if(allLines[i][j].name === startPoint) { 

      var startIndex = allLines.indexOf([i][j]); 
      return(startIndex); 
      } 
     return -1; 
     } 
    } 

}; 

function findEnd(array, endPoint) { 

    for(var k = 0; k< allLines.length; k++) { 

     for(var l = 0; l < allLines.length[k]; l++) { 

      if(allLines[i][j].name === endPoint) { 

       var endIndex = allLines.indexOf[k][l]; 

       return(endIndex) ; 
      } 
      return -1; 
      } 
     } 

     } 
    } 

    //Need to review 
    var partOne = function(startPoint, stop) { 

    var commonStopIndex = allLines.indexOf('UnionSquare'); 

    for (i = 0 ; i <= commonStopIndex ; i++) { 

     var tripOne = [allLines.slice(startPoint, commonStopIndex)]; 
     console.log (tripOne); 
    } 

    }; 

Antwort

0

Dies ist einer, es zu tun. Ich habe alles in eine Selbstabschaltung gestellt, so dass es beim Laden des Codes ausgelöst wird, aber Sie können es in eine benannte Funktion einfügen, wenn Sie es auf eine andere Weise auslösen möchten.

(function(){ 
// Create an array of lines. 
var lines = ['line1', 'line2', 'line3']; 

// Create a two dimensional arrat of the station names. 
var stations = [['Times Square', '34th', '28th', '23rd', 'Union Square', '8th'], 
['6th', 'Union Square', '3rd', '1st'], 
['Grand Central', '33rd', 'Union Square' , 'Astor Place']]; 

// Setup the other variables 
var getStrLn, getStrPnt, getEndLn, getEndPnt, setStrLn, setStrPnt, setEndLn, setEndPnt; 

// Create a series of window promts to collect and store user input. 
getStrLn = prompt('What line are you starting at?'); 

// Get the index positions for the line so it can be used in the next prompt. 
setStrLn = lines.indexOf(getStrLn); 

getStrPnt = prompt('What station are you going to on line ' +lines[setStrLn]+ '?'); 
getEndLn = prompt('What line are you going to end at?'); 

// Get the index positions for the line so it can be used in the next prompt. 
setEndLn = lines.indexOf(getEndLn); 
getEndPnt = prompt('What station are you going to end at on line ' +lines[setEndLn]+ '?'); 


// Get the index positions of the station that is entered. 

setStrPnt = stations[setStrLn].indexOf(getStrPnt); 

setEndPnt = stations[setEndLn].indexOf(getEndPnt); 

// Alert the results. 
alert('Your starting line is ' +lines[setStrLn]+ ' and your station is ' +stations[setStrLn][setStrPnt]+ '.\n Your ending line is ' +lines[setEndLn]+ ' and your ending station is ' +stations[setEndLn][setEndPnt]+ '.'); 
}()); 
Verwandte Themen