If statement help plz

When an image is clicked I want X to have a random value from 1 to 13
then depending on X a hidden image appears:
if X=2 : image 2 is shown
if X=5: image 5 is shown
etc

export function image1_click ( event ) {

**var**  X = 1 ; 
**function**  getRandomArbitrary ( min ,  max ) { 

X = Math . random () * ( 11 - 1 ) + 1 ;
}

 **if**  (X= 1 )  
                        $w ( "#image1" ). show (); 
                        
                    
                    **else if**  (X= 2 ) 
                     { 
                        $w ( "#image2" ). show ();} 
                            **else if**  (X= 3 ) 
                     { 
                        $w ( "#image3" ). show ();} 
                            **else**   
                     { 
                        $w ( "#image4" ). show ();} 

}

function getRandomArbitrary(min,max){   
	randomValue= Math.random()*(11-1)+1; //--> your generated random digit here
	$w(`#image${randomValue}`).show();	
}

export function image1_click ( event ) { function getRandomArbitrary ( min , max ){
randomValue = Math . random ()*( 11 - 1 )+ 1 ; //–> your generated random digit here
$w ( #image ${ randomValue }). show ();
}

}
gives me an error "cannot find name ‘randomValue’

My fault…

update the following codeline…

let randomValue = Math.random()*(11-1)+1;

Also try to console-log your random generated results…

console.log(randomValue);

im sorry im pretty much a beginner. where do I put the console log code line

export function image1_click ( event ) {

**function**  getRandomArbitrary ( min , max ){    
**let**  randomValue =  Math . random ()*( 11 - 1 )+ 1 ;  //--> your generated random digit here 
console . log ( randomValue ); 
$w ( `#image ${ randomValue }`). show ();   

}
}

I did this but nothing shows up when the trigger is clicked

Ok, at first → FORGET ABOUT THE USAGE OF EXPORT FUNCTIONS!

Instead use the following coding-style…

$w.onReady(()=>{$w('#image1').onClick(event){// ----> YOUR CODE HERE.....});}); 
$w.onReady(()=>{
   $w('#image1').onClick(event){console.log("Click")
       let randomValue = getRandomArbitrary(min,max);
       console.log("RETURNED-RANDOM-VALUE: ", randomValue);
       $w(`#image${randomValue}`).show();
   }); 
}); 

function getRandomArbitrary(min,max){
   return Math.random()*(11-1)+1; 
}

You hand over min and max values, but you do not use them inside of your function!

How and where to check the RESULTS inside of —> CONSOLE ?

If you are using → GOOGLE-CHROME → Press F12 and navigate to → CONSOLE.
If you are working on Wix-Editor → start the PREVIEW-MODE and open the CONSOLE on the very bottom of the Wix-Editor.

Investigate what happens, when you do actions.

EDIT:
Let’s improve our code and eleminate all little syntax-errors…

$w.onReady(()=>{
    $w('#image1').onClick((event)=>{
        console.log("Click");
        let min = 1, max = 10;

        let randomValue = getRandomArbitrary(min,max);
        console.log("RETURNED-RANDOM-VALUE: ", randomValue);
        $w(`#image${randomValue}`).show();
    }); 
}); 

function getRandomArbitrary(min,max){
   return Math.random()*(11-1)+1; 
}

Let’s take this pic , to set it as our click-trigger - - > ID = $w(‘#image1’)

As you can see, i just defined min and max in hardcoded mode… for testing…

let min = 1, max = 10;

So here you can see the returned results inside of the console-log.
Here you can emmidiatelly recognize – > that your function will never work like that!

WHY ???

Because you get some
decimal numbers instead of integers (normal numbers)

But what’S exactly the issue?

The issue is → that you don’t have a pic on your page with the ID-Ending of - - > for example - - → RETURNED-RANDOM-VALUE: 9.183038827734011

You need clear fixed and defined NUMBERS like → 1,2,3,4,5,6,7,8,9

if(randomValue===1) {...}
else if(randomValue===2)  {...}
else if(randomValue===3)  {...}
else if(randomValue===4)  {...}
else {...}

...or the direct way....

$w(`#image${randomValue}`).show();

So you have first to round the returned result-number.
The Math.abs() Method
The Math.ceil() Method
The Math.floor() Method
The Math.round() Method
The Math.fround() Method
The Math.trunc() Method

JavaScript Math round() Method.

Now first make some brainstorming and learn the stuff, before you continue…

Choose the right rounding-function for your own needs…

I feel like there should be a way to control the returned value and make it a solid number but fine, can I skip the rounding and make multiple if statements as intervals like:
if (1> randomValue>2 ){ … }
elseif (2> randomValue>3 ){ … }

im sure that’s not the correct way to put it so help me xD thx.

about the way, I understand the code perfectly, I just dont know how to type everything in a correct way (but after a bit of research I get it done) and sometimes I just dont know what code I need so yea, I can read and understand, but not fluent in writing

aight i think i got it working
export function image1_click ( event ) {

console . log ( “Click” );
let min = 1 , max = 5 ;

    **let**  randomValue  =  getRandomArbitrary ( min,max ); 
    console . log ( "RETURNED-RANDOM-VALUE: " ,  randomValue ); 

function getRandomArbitrary ( min,max ){
return Math . random ()*( 5-1 )+ 1 ;
}

**if**  ( randomValue > 1  &&  randomValue< 2 ) { $w ( '#image4' ). show ();} 
**else if**  
( randomValue > 2  &&  randomValue< 3 ) { $w ( '#image5' ). show ();} 
    **else if**  
( randomValue > 3  &&  randomValue< 4 ) { $w ( '#image3' ). show ();} 
    **else**  
{ $w ( '#image2' ). show ();} 

}

And you still use the → EXPORT FUNCTION :rofl:
Ok, if you like it to code that way → why not!

Why not ???

  1. Because such EXPORT FUNCTIONS needs connections inside of property-panel, which can be broken, and you start to search for ERROR and maybe won’t be able to find the issue.

  2. Because using such export functions, will give you headaches if it comes to the usage of onReady-commands/events.

  3. and surely there are more aspects i could tell you, why not to go the way you already going :grin:

But well → THE MAIN AND MOST IMPORTANT THING IS —> THAT IT WORKS FOR YOU LIKE YOU EXPECT IT.

So if everything works like you expected → congratulation !

that was just testing, i’ll try to use the onready. and I think im facing that problem on a different thing, i think it broke, when I run the code, one time 3 videos show up and another time it works fine and another time only 1 shows up (its supposed to show 4 videos after a delay)
export function image28_click ( event ) {

setTimeout (() => { 
    $w ( '#videoBox10' ). show (),  $w ( '#videoBox15' ). show (), 

        $w ( '#videoBox14' ). show (),  $w ( '#videoBox11' ). show (); 

},  5000 ); 

setTimeout (() => { 
    $w ( '#videoBox10' ). play (),  $w ( '#videoBox15' ). play (); 

},  5100 ); 

setTimeout (() => { 
    $w ( '#videoBox14' ). play (),  $w ( '#videoBox11' ). play (); 

},  5300 ); 

setTimeout (() => {
$w ( ‘#videoBox5’ ). show (), $w ( ‘#videoBox7’ ). show (), $w ( ‘#videoBox8’ ). show (), $w ( ‘#videoBox9’ ). show ();

    },  7900 )  ; 

 setTimeout (() => { 
     $w ( '#videoBox5' ). play (), $w ( '#videoBox7' ). play (), $w ( '#videoBox8' ). play (), $w ( '#videoBox9' ). play (); 

    
  
},  8100 )  ; 

}

never mind…its…working again… i dont even