0
var videoinfo = [{
    "startdatetime":"2014-12-21 00:23:14",
    "totalsecondrun":"5",
    "videolist":[
        {
            "videoid":"uoSDF234",
            "second":"10"
        },
        {
            "videoid":"0apq3ss",
            "second":"14"
        }
    ]
}];

I need a function to calculate current video and second, but so far I don't have any idea how to start.

  1. use totalsecondrun to find currently playing which video
  2. base on totalsecondrun is between which video now.
  3. exmaple: totalsecondrun:"5", 5 so should be the first video, function return videoid and second (totalsecondrun:"5" - first video second = video stopped second)
  4. example: totalsecondrun:"12", 12 is more than first video, so now it should be second video, so function return videoid and second too.

Anyone can suggest me what should I do ?

4
  • can you be more clear . seems you already have solution to your problem . what you are not able to do ? Commented Jan 8, 2015 at 6:57
  • @shifu dont know how to write ... Commented Jan 8, 2015 at 6:59
  • 1
    What is the exact problem you are facing, apart from the logic? Are you asking how to access values from the JSON? Commented Jan 8, 2015 at 7:01
  • @surajck how to compare and match ? Commented Jan 8, 2015 at 7:08

1 Answer 1

1

see if this is what you want to do .

var videoinfo = [{
        "startdatetime":"2014-12-21 00:23:14",
        "totalsecondrun":"5",
        "videolist":[
            {
                "videoid":"uoSDF234",
                "second":"10"
            },
            {
                "videoid":"0apq3ss",
                "second":"14"
            }
        ]
    }];
    getVedioID();
    function getVedioID(){
        var sum = 0;
        var list = videoinfo[0].videolist;
        for(var i=0 ; i<list.length ; ++i){
            sum+=parseInt(list[i].second);
            if(sum > parseInt(videoinfo[0].totalsecondrun)){
                var ans = {};
                ans['curr'] = list[i].videoid;
                ans['next'] = list[i+1].videoid;
                break;
            }
        }
        return ans;
    } 
Sign up to request clarification or add additional context in comments.

4 Comments

if I change the totalsecondrun to 11 mean more than first video second it will error
OK I have modify some of your code to what I expected so far, now my problem only left one. second part , if i set totalsecondrun is 5 the ans should give me the rest 5 also. if i set totalsecondrun to 11 it should give me the 13
i just showed how to handle json for your case . error handling hope you can do self.
OK, I have done what I want because of your suggestion code, I tried to keep modify what I need, finally I did ! Thanks you so much !

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.