lundi 5 décembre 2016

Unable to get the file path name after randomising

Functionality:

An array of image file path element will be randomised, and randomised image will append to the image tag.

Issue:

I have randomised the array of image element and when I console.log the randomised image, it returns the index of the array. At this point, the returned appended image error msg will be "lib/PAGE08/1" cannot be found, error404.

Hence, I used parseInt() method on the randomised element. However, it did not return the image path name, but still returned the index of the randomised element:

Meaning:

randomised method : var randomCookies = Math.floor(Math.random() * goodcookies.length);

when I console.log console.log("randomCookies: "+ randomCookies);, it returns the index, for example: 1, which points to the index 1 of the array "goodcookies"

I have tried to use parseInt() to convert the index back to the file path name: showgoodcookies = parseInt([randomCookies.length]); and when I console.log(), it is still showing the index.

Code:

var goodcookies = ["lib/PAGE08/Nutrients.png", "lib/PAGE08/BlueNutrients.png", "lib/PAGE08/RedNutrients.png"];
var showgoodcookies = "";


function spawnGood() {
  //Randomise cookies
  var randomCookies = Math.floor(Math.random() * goodcookies.length);
  console.log("randomCookies: " + randomCookies);
  showgoodcookies = parseInt([randomCookies.length - 1]);
  console.log("showgoodcookies: " + showgoodcookies);

  var goodNutrient = document.createElement("img");
  goodNutrient.id = "test1";

  //goodNutrient.src = "lib/PAGE08/Nutrients.png";
  goodNutrient.src = showgoodcookies;
}

Hence, I do require help, as in what went wrong when I tried to use parseInt() to convert the index back to the file path name of the image.




Aucun commentaire:

Enregistrer un commentaire