Need help with this code


#1


I tried to create a code just to exercise but something is missing.


In the code i wanted to generate a number with Math.random and if it was < 0.5 it should have printed the array from 1 to 6 , in the other case ( number > 0.5) from 6 to 1.
Something is missing and the console only prints the number created by Math.random and =>6


var lellus = [1,2,3,4,5,6];
var computerChoice = Math.random ();
console.log(computerChoice);
var A = function(computerChoice) {
if(0 < computerChoice < 0.5) {
    for(i = 0; i < lellus.length; i++) {
    return lellus[i];
}
}
else {
    for(i = 5; i >= 0; i--){
    return lellus[i];
}
}
};
A(computerChoice);


#2

return lellus[i];

return terminates a function whenever a return keyword is reached, function ends at that moment.so use console.log instead of return.
and here

if(0 < computerChoice < 0.5) {

simply you can write if(computerChoice < 0.5) because the random() method returns a random number from 0 (inclusive) up to but not including 1 (exclusive).


#3

ok perfect, thank you!
so if I use the return keyword it will only print to the console the last item of the process?


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.