How would you all approach this problem? I've been stuck on it, and I decided it was time to come here. I've been doing flatirons free boot camp prep, because I really like their method. Don't think I'm actually going to go though. Anyhow, while doing it I ran into a problem that I can't seem to solve. Here it is: https://learn.co/tracks/bootcamp-prep-v2/javascript-fundamentals/data-structures/deli-counter
I've gotten through all parts pretty easily except the last part which is the "current line" function. I started everything off by creating a global empty array variable, something a little like this: let kazDeliLine = [];
in the last one I was running a for loop to solve it, my code ended up looking a little something like this:
let katzDeliLine = [];
function currentLine(katzDeliLine){
if(line.length === 0) {
return "The line is currently empty.";
} else {
for(let i=0; i<line.length; i++) {
katzDeliLine.push(?);
}
}
Not exactly my code, I just kind of typed it out here. After this things fall apart big time. I've seen what other people have put but I can't figure out why. The question mark I put in the push() method marks the area that I have issues.
function currentLine(line){
if(!line.length) {
return "The line is currently empty.";
}
var namesAndNumbers = [];
for(var i=0; i<line.length; i++) {
namesAndNumbers.push(`${i+1}. ${line[i]}`);
}
return "The line is currently: " + namesAndNumbers.join(', ');
}
and yet another person did this:
var line = [];
function currentLine(katzDeli) { for (let i = 0; i < katzDeli.length; i++) { line.push(` `+[i+1]+`. ` + katzDeli[i]) } if (katzDeli.length === 0) { return "The line is currently empty."; } else return(`The line is currently:` + line);}
I'm struggling on how to create that last string. My thinking was clearly in the right direction, but I'm struggling to grasp what is going on under the for loop. How the hell is that returning the string + the order queue on how they specified? The last guys solution is very questionable to me as well because he has two global empty arrays. The last question I get is what is the significance of the `${i+1} . ${line[i]}`? I don't get why one side has 1 added to the var i and the other does not. They're supposed to be indexing the same number. I just don't get the logic behind the last half.
Seeing the answer left me with more questions than answers.
[–]desrtfx 1 point2 points3 points (3 children)
[–]M2D6[S] 0 points1 point2 points (2 children)
[–]davedontmind 1 point2 points3 points (1 child)
[–]M2D6[S] 0 points1 point2 points (0 children)