Home > Enterprise >  I'm trying to make function randomly choose between the 2 images but nothing happens
I'm trying to make function randomly choose between the 2 images but nothing happens

Time:01-08

the 2 images are uploaded in the coding program

Function getFoto() {
  var Foto = new Array();
Foto[0] = "image1.jpg";
Foto[1]=  "image2.jpg";

var Nummer =  Math.floor(Math.random()*Foto.length);
  return document.getElementById("result").innerHTML = '<img src="' Foto[Nummer] '" />';}   
getFoto()

CodePudding user response:

You make the function keywords to Function which will not be executed and parsed by JS.

function getFoto() {
  var Foto = new Array();
Foto[0] = "image1.jpg";
Foto[1]=  "image2.jpg";
   
var Nummer =  Math.floor(Math.random()*Foto.length);
console.log(Nummer)
  return document.getElementById("result").innerHTML = '<img src="' Foto[Nummer] '" />';
  }   
getFoto()
<div id=result></div>

CodePudding user response:

As with all things in programming, learning to debug is essentially the primary skill you need to solving most problems. So in this situation I would first setup debugging in vscode and then look at the value you are passing in the math.Floor function. Then look at the value of Nummer. Then make sure Foto[that value of Nummer] works.

  •  Tags:  
  • Related