James Bond

Page section: playground

jamesBondFilms.length stored in a variable.

There were 25 films. to see who starred.

uses the forEach method on jamesBondFilms.

Code here uses jamesBondFilms which is an array of objects, each of which is the name of a Bond film. Each object contains the following 5 properties:

  1. title of the film
  2. year the film was released
  3. actor who played James Bond
  4. num the number of the film (first film = 1).
  5. bondGirl the main female character of the film
  6. imdb the IMDB rating for the film

These are stored in the pure-data.js file. The objects were created using an object constructor function:

function BondFilm(title, year, actor, num, bondGirl, imdb) {
  (this.title = title),
  (this.year = year),
  (this.actor = actor),
  (this.num = num),
  (this.bondGirl = bondGirl),
  (this.imdb = imdb);
}

The this keyword refers to an empty object.

Next each bond film object is created using the above arguments:


const drNo = new BondFilm(
  "Doctor No",
  1962,
  "Sean Connery",
  1,
  "Ursula Andress",
  7.2
);

The keyword new does 3 things:

  1. It creates a new empty object.
  2. It sets this to point to this object
  3. It returns that object from the function
function titleList() {
    const filmList = document.getElementById('film-list');
        filmList.innerHTML = '';
        let i = 0;
    jamesBondFilms.forEach((item) => {
        i = i+ 1;
        console.log(item.title);
        filmList.innerHTML += `${i}. ${item.title} (${item.year}) with ${item.actor}.<br>`;
    });
};

Generating a random number

The random Bond girl button works like this:

  1. Firstly generates a random number using Math.random().
  2. As the number is a decimal between 0 and 1 multiply it by 100.
  3. Since we only need numbers between 0 and 24 (25 films) divide by 4
  4. 100/4 = 25 hence multiplying by 25.
  5. Finally we only want whole integers so Math.Round is used to round up or down to the nearest whole integer.
  6. This is stored in a variable called randomFilmNum which is used as the index of the array of Bond films, jamesBondFilms.
let randomFilmNum = Math.round(Math.random() * 25);
alert(jamesBondFilms[randomFilmNum].bondGirl);

Note that the variable randomFilmNum is declared inside the onclick declaration so that it is re-run every click of the button. If it was declared outside, in a <script> tag before the button it would only be generated once when the page loads rather than every click of the button.

Conclusion

order

The JS here is in 3 places: in the HTML (using onclick), in <script> tags at the bottom of the page and in an external file pure-data.js which is linked in the <head> of the page.

Variables declared in the bottom <script> tags don’t work in the HTML onclick attributes.

Film titles

The Bond films are stored in an array of objects each of which has a different name, eg. noTimeToDie. Converting these camel case names would be a pain so it’s useful to include a title property in each object too.

View contents of: /static/js/pure-data.js
// This file contains no programming just objects, variables and arrays

const primates = [
  "mangabey",
  "drill",
  "colobus",
  "gelada",
  "linsang",
  "lutung",
  "talapoin",
  "tamarin",
  "titi",
  "guenon",
  "guereza",
  "kipunji",
  "grivet",
];

const viverids = ["binturong", "civet", "falanouc", "genet"];

const dinosaurs = [
  {
    name: "Baryonyx",
    meaning: "Heavy claw",
    family: "Spinosaur",
    period: "Early cretaceous",
    country: "UK",
    year: 1983,
  },
  {
    name: "Bolong",
    meaning: "long dragon",
    family: "Hadrosaur",
    period: "Early cretaceous",
    country: "China",
    year: 2010,
  },
  {
    name: "Opisthocoelicaudia",
    meaning: "posterior cavity tail",
    family: "Titansaur",
    period: "Late cretaceous",
    country: "Mongolia",
    year: 1965,
  },
  {
    name: "Coelophysis",
    meaning: "Hollow form",
    family: "theropod",
    period: "Late triassic",
    country: "USA",
    year: 1881,
  },
  {
    name: "Epidexipteryx",
    meaning: "Hu Yaoming's dragon",
    family: "theropod",
    period: "Jurassic",
    country: "Inner Mongolia",
  },
];

// Bond films using object constructor

function BondFilm(title, year, actor, num, bondGirl, imdb) {
  (this.title = title),
    (this.year = year),
    (this.actor = actor),
    (this.num = num),
    (this.bondGirl = bondGirl),
    (this.imdb = imdb);
}

const drNo = new BondFilm(
  "Doctor No",
  1962,
  "Sean Connery",
  1,
  "Ursula Andress",
  7.2
);

const fromRussiaWithLove = new BondFilm(
  "From Russia with Love",
  1963,
  "Sean Connery",
  2,
  "Daniela Bianchi",
  7.4
);

const goldfinger = new BondFilm(
  "Goldfinger",
  1964,
  "Sean Connery",
  3,
  "Honor Blackman",
  7.7
);

const thunderball = new BondFilm(
  "Thunderball",
  1965,
  "Sean Connery",
  4,
  "Claudine Auger",
  7
);

const youOnlyLiveTwice = new BondFilm(
  "You Only Live Twice",
  1967,
  "Sean Connery",
  5,
  "Akiko Wakabayashi",
  6.8
);

const onHerMajestysSecretService = new BondFilm(
  "On Her Majestys Secret Service",
  1969,
  "George Lazenby",
  6,
  "Diana Rigg",
  6.7
);

const diamondsAreForever = new BondFilm(
  "Diamonds are Forever",
  1971,
  "Sean Connery",
  7,
  "Jill St John",
  6.6
);

const liveAndLetDie = new BondFilm(
  "Live and Let Die",
  1973,
  "Roger Moore",
  8,
  "Jane Seymour",
  6.8
);

const theManWithTheGoldenGun = new BondFilm(
  "The Man with the Golden Gun",
  1974,
  "Roger Moore",
  9,
  "Britt Eckland",
  6.7
);

const theSpyWhoLovedMe = new BondFilm(
  "The Spy Who Loved Me",
  1977,
  "Roger Moore",
  10,
  "Barbara Bach",
  7.1
);

const moonraker = new BondFilm(
  "Moonraker",
  1979,
  "Roger Moore",
  11,
  "Lois Chiles",
  6.3
);

const forYourEyesOnly = new BondFilm(
  "For Your Eyes Only",
  1979,
  "Roger Moore",
  12,
  "Carole Bouquet",
  6.7
);

const octopussy = new BondFilm(
  "Octopussy",
  1983,
  "Roger Moore",
  13,
  "Maud Adams",
  6.5
);

const aViewToAKill = new BondFilm(
  "A View to a Kill",
  1985,
  "Roger Moore",
  14,
  "Tanya Roberts",
  6.3
);

const theLivingDaylights = new BondFilm(
  "The Living Daylights",
  1987,
  "Timothy Dalton",
  15,
  "Maryam d'Abo",
  6.7
);

const licenceToKill = new BondFilm(
  "Licence To Kill",
  1989,
  "Timothy Dalton",
  16,
  "Carey Lowell",
  6.6
);

const goldenEye = new BondFilm(
  "Golden Eye",
  1995,
  "Pierce Brosnan",
  17,
  "Izabella Scorupco",
  7.2
);

const tomorrowNeverDies = new BondFilm(
  "Tomorrow Never Dies",
  1997,
  "Pierce Brosnan",
  18,
  "Michelle Yeoh",
  6.5
);

const theWorldIsNotEnough = new BondFilm(
  "The World is Not Enough",
  1999,
  "Pierce Brosnan",
  19,
  "Sophie Marceau",
  6.4
);

const dieAnotherDay = new BondFilm(
  "Die Another Day",
  2002,
  "Pierce Brosnan",
  20,
  "Halle Berry",
  6.1
);

const casinoRoyale = new BondFilm(
  "Casino Royale",
  2006,
  "Daniel Craig",
  21,
  "Eva Green",
  8
);

const quantumOfSolace = new BondFilm(
  "Quantum of Solace",
  2008,
  "Daniel Craig",
  22,
  "Olga Kurylenko",
  6.6
);

const skyfall = new BondFilm(
  "Skyfall",
  2012,
  "Daniel Craig",
  23,
  "Naomie Harris",
  7.8
);

const spectre = new BondFilm(
  "Spectre",
  2015,
  "Daniel Craig",
  24,
  "Léa Seydoux",
  6.8
);

const noTimeToDie = new BondFilm(
  "No Time to Die",
  2021,
  "Daniel Craig",
  25,
  "Ana de Armas",
  7.4
);

const jamesBondFilms = [
  drNo,
  fromRussiaWithLove,
  goldfinger,
  thunderball,
  youOnlyLiveTwice,
  onHerMajestysSecretService,
  diamondsAreForever,
  liveAndLetDie,
  theManWithTheGoldenGun,
  theSpyWhoLovedMe,
  moonraker,
  forYourEyesOnly,
  octopussy,
  aViewToAKill,
  theLivingDaylights,
  licenceToKill,
  goldenEye,
  tomorrowNeverDies,
  theWorldIsNotEnough,
  dieAnotherDay,
  casinoRoyale,
  quantumOfSolace,
  skyfall,
  spectre,
  noTimeToDie,
];