Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2018
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    the given javascript code is not executing

    this code was meant to print data from any array objects by where the entered season and episode matched with the data entered but this code is not executing

    Code:
    let obtain=(season,episode,info)=>
    
    {
      var seasonfound=false
      var episodefound=false
      for (var i of info)
      {
        if(info[i][season]==season)
        {
          if(info[i][episode]==episode)
          {
            seasonfound=true
            episodefound=true
             break
          }
          else{
            seasonfound=true
            episodefound=false
            break
          }
        }
        else
        {
          seasonfound=false
        }
      }
      if (seasonfound==true && episodefound==true)
      {
    
        alert(info[i].id)
      }
      else{
        alert('incorrect')
      }
    
       
    
         }
    
     
    
    let S=window.prompt('enter the seaseon you wish')
    
    let E=window.prompt('enter the episode you wish')
    
    obtain(S,E,information)
    Last edited by vinyl-junkie; Jul 12th, 2018 at 05:32 PM. Reason: added code tags

  2. #2
    New to the CF scene
    Join Date
    Jun 2018
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    this is the link to the javascript data set
    https://s3-ap-southeast-1.amazonaws....assignments/we
    b03/JSS1l2/bigbangtheory.json

  3. #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    TX
    Posts
    3,919
    Thanks
    64
    Thanked 601 Times in 595 Posts
    Difficult to determine the problem as the link does not show any useful information.

    Strickly a guess, but prompt is a poor way to acquire user information.
    1. Are the entries spelled correctly?
    2. Is the case (upper/lower) matched?
    3. Do you have spaces in the entries?

  4. #4
    Regular Coder
    Join Date
    Apr 2011
    Posts
    187
    Thanks
    0
    Thanked 58 Times in 55 Posts
    You probably want to compare to the season and episode keys, instead of the value passed for season / episode.
    Code:
    var key = 'episode';
    object.episode;
    object['episode'];
    object[key];
    A for-of loop doesn't return the key of a property, but it's value. You can also combine your test conditions into one 'if' statement.
    Code:
    function obtain(season, episode, info) {
      for(var o of info) {
        if(o.season == season && o.episode == episode) {
          alert(info[i].id);
          break;
        }
      }
      alert('incorrect');
    };
    And ofcourse your browser needs to be ECMA 2016 compatible.

  5. #5
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,700
    Thanks
    26
    Thanked 584 Times in 577 Posts
    And for completeness' sake, the same with array functions:
    PHP Code:
    var item info.filter(=> i.season === season && i.episode === episode).shift() 
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  6. #6
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,086
    Thanks
    4
    Thanked 447 Times in 436 Posts
    1) stunning example of why I don't use let and don't use arrow functions.

    2) are you sure the browser you're testing in supports for/of? That's pretty new... and so new you're using it wrong. "var i of info" would return the entry/value, NOT an index into info, so everyplace you have "info[i]" should just be "i".

    3) the assignment logic inside the loop might be overwriting the result state. IF it finds the season with episode before it finds the episode, then one of the season without the episode, the episode state will be lost.

    4) I'd also just short circuit out once all conditions are true. Rather than break, just return!

    I'd dial things back to be compatible in all browsers, so something more like:

    Code:
    function obtain(season, episode, info) {
    	for (var i = 0, seasonFound = false, line; line = info[i]; i++) {
    		if (line.season == season) {
    			if (line.episode == episode) return 'Episode Found';
    			seasonFound = true;
    		}
    	}
    	if (seasonFound) return 'Season Found, No Episode';
    	return 'Season and Episode not found.';
    }
     
    
    var
    	s = window.prompt('enter the seaseon you wish')
    	e = window.prompt('enter the episode you wish')
    
    alert(obtain(s, e, information));
    ... assuming the value in 'information' is correct.

    If you insist on using for/of that would go like this:

    Code:
    function obtain(season,episode,info) {
    	var seasonFound = false;
    	for (var line of info) {
    		if (line.season == season) {
    			if (line.episode == episode) return 'Episode Found';
    			seasonFound = true;
    		}
    	}
    	if (seasonFound) return 'Season Found, No Episode';
    	return 'Season and Episode not found.';
    }
    'of' doesn't work like 'in'. (as @goos posted)
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  7. Users who have thanked deathshadow for this post:

    adity1314 (Jul 19th, 2018)


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •