1

I am trying to make this work but I am clearly doing something wrong which I can't figure out:

function template(data){

  var contentString = 
    '<div id="content">'+
    '<div class="left">'+
    "<div class='icon'><img src='assets/image/cloudy.png' /></div>"+
    '</div>'+
    '<div class="right">'+
    '<p class="temp">'+data.currently.temperature+'</p>'+
    '<p class="summary">'+data.daily.summary+'</p>'+
    '</div>'+
    '<div class="day_wrapper">'+

    for(i = 0; i < data.daily.data.length; i++){
       '<div class="day"><p>'+data.daily.data[i].temperatureMax+'</p></div>'+
    }

    '</div>';

    return contentString;

} 

Error: Uncaught SyntaxError: Unexpected token for

How can I fix this?

How would I add a 'last' class to the last paragraph in the loop?

5
  • 4
    You can't do that. You need to do contentString += inside your for loop. Remove the + just before the loop and add contentString += before your last div. Commented Nov 13, 2013 at 12:36
  • 1
    you can't use for while concatenating. End you concatenate before the for loop , continue inside it, then continue after it. I also would strongly advice the use of a templating framework like mustache.js for example... Commented Nov 13, 2013 at 12:38
  • 1
    have you considered using templates - this stuff gets ugly fast Commented Nov 13, 2013 at 12:39
  • Hi all, yes I have considered using handlebars or underscore, however at the moment I am only playing around with data coming from the API. Commented Nov 13, 2013 at 12:40
  • fair enough; looks like @harshboss has a precise answer ;) Commented Nov 13, 2013 at 12:42

1 Answer 1

3
function template(data){

  var contentString = 
    '<div id="content">'+
    '<div class="left">'+
    "<div class='icon'><img src='assets/image/cloudy.png' /></div>"+
    '</div>'+
    '<div class="right">'+
    '<p class="temp">'+data.currently.temperature+'</p>'+
    '<p class="summary">'+data.daily.summary+'</p>'+
    '</div>'+
    '<div class="day_wrapper">';

if(data!=undefined && data.daily.data.length > 0)
{
        for(i = 0; i < data.daily.data.length; i++){
    contentString +=    '<div class="day"><p>'+data.daily.data[i].temperatureMax+'</p></div>';
        }
 }  


contentString += '</div>';

    return contentString;

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

8 Comments

But please you also need to check data as undefine or not null
Sorry i am still fairly new to data coming from APIs, what is the benefit of checking if data is undefined or null? :O
if there is not any data comes from api then it will come exception. so for handle that exception or strong coding side must check as data coming as undefined or not ?
If data is null then its not read data.daily.data.length line
So, if you're going to check that data is defined, you should probably also be checking that all the structures you're referencing within data are also defined. E.g. if (data && data.daily && data.daily.data && ... :-/ As for why you do this, it's good practice to insure you're not trying to reference a property on an undefined or null object (in this case, data). The only reason you wouldn't do that is if you know for sure that data (and all it's sub-members) are going to have the expected [defined] properties.
|

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.