Vishnu
Vishnu

Reputation: 2452

Storing random number in old and new string

This is hard to explain; please ask me in comments if you cannot understand my question.

Please see the below code. First, I am generating random text from an array, then I have two div elements: current and old div. What I am trying to achieve is when start button is first pressed, any random text can be shown in current and old div. When the start button is pressed a second time, whichever text appeared in old div the first time should be shown in current div and any random text is shown in old div and loop goes on.

I tried to initialize old with 0 , but it is showing as undefined inside function.

<script>
    function randomFromTo(from, to) {
        return Math.floor(Math.random() * (to - from + 1) + from);
    }
    var arrstring = new Array("images", "wallpaper", "photos", "vector",
        "designer", "wordpress", "jquery", "extjs", "scripting", "blogging", 
        "search", "tagging", "digital", "javascript", "server", "hosting",
        "social", "twitter", "graphic", "photoshop", "netbeans", "mysql",
        "apache", "iphone", "mobile", "android", "framework", "usability",
        "optimization", "interface", "developer");

    function random() {
        randomIndex = randomFromTo(0, arrstring.length - 1);
        return randomIndex;
    }

    var old = 0;
    function start() {
        var all = old;
        var old = random();
        $(".current").text(arrstring[all]);
        $(".old").text(arrstring[old]);
    }
</script>

<div class="current">old</div>
<div class="old">new</div>

<input type="button" onclick="start();" value="start" />

Upvotes: 2

Views: 131

Answers (5)

saun4frsh
saun4frsh

Reputation: 383

Use of global and local variable with same name.. only remove var to local variable.. It will give different meaning inside of that function..

    var old = 1; // Global
    function start() {
    var all = old; //local
    var old = random(); //local..
    ..}

Upvotes: 0

Benjamin Warren
Benjamin Warren

Reputation: 539

This will give you the same functionality in a cleaner way, but not polluting the global scope.

jsFiddle

function get_Random_String() {

    var arrstring = new Array("images", "wallpaper", "photos", "vector",
        "designer", "wordpress", "jquery", "extjs", "scripting", "blogging",
        "search", "tagging", "digital", "javascript", "server", "hosting",
        "social", "twitter", "graphic", "photoshop", "netbeans", "mysql",
        "apache", "iphone", "mobile", "android", "framework", "usability",
        "optimization", "interface", "developer");

    return arrstring[Math.floor(Math.random() * arrstring.length)];

}

function cycle_randoms() {
    $(".current").text($(".old").text());
    $(".old").text(get_Random_String());
}


$('input[value=start]').on('click', function () {
    cycle_randoms();
});

Upvotes: 2

JohnnyJS
JohnnyJS

Reputation: 1472

Please Use jQuery built-in procedures.

you should use doc ready before applying changes to dom:

$(document).ready(function(){
       /*ALL JS CODE HERE.*/
    });

try to use jQuery event listener:

$("button").click(function(){
   /*here is 'start' function */
});

optional: put all js code in code.js files, and put them inside your head section.

Upvotes: -1

DACrosby
DACrosby

Reputation: 11460

Looks like you're close! Just having some variable scope issues. Try something like this:

function start() {
  var all = random();
  $(".current").text(arrstring[all]);
  $(".old").text(arrstring[old]);
  old = all;
}

http://jsfiddle.net/daCrosby/zbSDC/

Upvotes: 1

Vishnu
Vishnu

Reputation: 2452

HI guys i solved problem myself , I just removed var and it worked successfully

 function start()
    {
    all = old;
    old = random();
    $(".current").text(arrstring[all]);
    $(".old").text(arrstring[old]);

    }

Upvotes: 2

Related Questions