Jump to content

nodeschool juggling async


Recommended Posts

hi , im trying to solve said exercise in nodeschool


JUGGLING ASYNC Exercise 9 of 13This problem is the same as the previous problem (HTTP COLLECT) in that you need to use http.get(). However, this time you will be provided with three URLs as the first three command-line arguments.You must collect the complete content provided to you by each of the URLs and print it to the console (stdout). You don't need to print out the length, just the data as a String; one line per URL. The catch is that you must print them out in the same order as the URLs are provided to you as command-line arguments.-------------------------------------------------------------------------------## HINTSDon't expect these three servers to play nicely! They are not going to give youcomplete responses in the order you hope, so you can't naively just print the output as you get it because they will be out of order.You will need to queue the results and keep track of how many of the URLs have returned their entire contents. Only once you have them all, you can print the data to the console.Counting callbacks is one of the fundamental ways of managing async in Node. Rather than doing it yourself, you may find it more convenient to rely on a third-party library such as [async](http://npm.im/async) or [after](http://npm.im/after). But for this exercise, try and do it without any external helper library.-------------------------------------------------------------------------------

first i solved it like this (first time it worked)

var http=require("http");var urls=process.argv.slice(2,process.argv.length);var count=0;var junge=[];urls.map(function(url,index){http.get(url,function(response){var str="";response.setEncoding("utf-8");response.on("data",function(data){str=str+data})response.on("end",function(){junge.push(str);count++;if(index==urls.length-1) junge.map(function(v){console.log(v)})});});})

but second and so on time it threw that i failed , so i thought maybe its printing in the wrong order? So before i prined it i sorted it. and final code looks like


var http=require("http");var urls=process.argv.slice(2,process.argv.length);var junge=[];var finaly=[];urls.map(function(url,index){http.get(url,function(response){var str="";response.setEncoding("utf-8");response.on("data",function(data){str=str+data})response.on("end",function(){junge.push({text:str,url:url});if(index==urls.length-1) {urls.forEach(function(m){var is=false;junge=junge.filter(function(x){if(!is && x.url==m){finaly.push(x);is=true;return false;}elsereturn true;})})finaly.forEach(function(y){console.log(y.text)})}});});})

the error just says i failed , but the result looks differently every time , here is the recent one


1.  ACTUAL:    "Grab us a fly wire no worries she'll be right rego. Trent from punchy trackies with flat out like a parma. As stands out like two pot screamer and you little ripper waratah. "1.  EXPECTED:  "Flat out like a two pot screamer with he hasn't got a ya. You little ripper flanno piece of piss built like a trackie dacks. He's got a massivehottie with get a dog up ya cranky. Built like a franger mate lets throw a gonewalkabout. "2.  ACTUAL:    ""2.  EXPECTED:  "Get a dog up ya rapt my as stands out like ute. Stands out likea chuck a sickie and get a dog up ya cactus mate. Stands out like a bitzer heaps she'll be right cubby house. Watch out for the bushranger no worries as cunning as a bitzer. As stands out like bushranger also gutful of smoko. "3.  ACTUAL:3.  EXPECTED:  ""────────────────────────────────────────────────────────────────────────────────? Submission results did not match expected!# FAILYour solution to JUGGLING ASYNC didn't pass. Try again!
any help?
Edited by Matej
Link to comment
Share on other sites

If you have a list of URLs, and a list of results, then the result for URL 0 should go in position 0 in the results, the result for URL 1 should go in position 1 in the results, etc. Don't just push the result onto the array, put it where it should go.

Link to comment
Share on other sites

thats it , its aint displaying in right order or somehing , the ordering script works i tested it , but somehow its broken or something , as i said i solved this exercisee first time with diferent script which aint working aymore , so ii dont know mistake is on my side or on their. Thats why i posted it . to find out if I did mistake.

Link to comment
Share on other sites

It should be easy enough to verify that it works correctly, that shouldn't be hard to do. Write the URL and content to the console each time a response comes back, and then you can compare at the end that you have the correct responses in the correct places for the correct URLs. I don't see why you need to re-order anything though when you can just put it in the right place to start with.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...