Need to make this non repeating random feed

Here is the code I have to select a random news feed. What I want is to modify this to pick a random feed, but if the feed picked is the current feed to pick again.

<script>
var feeds = [
'http%3A%2F%2Ffeeds.skynews.com%2Ffeeds%2Frss%2Fus.xml',
'http%3A%2F%2Ffeeds.bbci.co.uk%2Fnews%2Fworld%2Frss.xml',
'https%3A%2F%2Fwww.nytimes.com%2Fsvc%2Fcollections%2Fv1%2Fpublish%2Fhttps%3A%2F%2Fwww.nytimes.com%2Fsection%2Fus%2Frss.xml',
'https%3A%2F%2Fwww.yahoo.com%2Fnews%2Frss',
'http%3A%2F%2Ffeeds.reuters.com%2FReuters%2FdomesticNews',
'http%3A%2F%2Ffeeds.washingtonpost.com%2Frss%2Frss_blogpost'
];

function randomFeed(feeds) {
   return feeds[Math.floor(Math.random() * feeds.length)];

}

var feed = randomFeed(feeds);

var link = "<script language='JavaScript' src='http://feedroll.com/rssviewer/feed2js.php?src=" + feed + "&chan=title&num=4&desc=1&targ=y&utf=y&html=y' charset='UTF-8' type='text/javascript'><\/script>";

</script>

I’m sure it could be done more succinctly, but maybe this?

const randomFeed = (function(){
    let prevIndex = null

    return function randomfeed(feeds){
        const newIndex = Math.floor(Math.random() * feeds.length)
        if (newIndex === prevIndex) return randomfeed(feeds)
        prevIndex = newIndex
        return feeds[newIndex] 
    }
}())

for (let i = 0; i < 100; i++) {
  console.log(randomFeed(feeds))  
}
1 Like

You could do it with a recursive call but you’ll need to pass the current feed to the call

(excuse the code since it’s been a while since I’ve done JS)

function randomFeed(feeds, currentFeed) {
   var newFeed = feeds[Math.floor(Math.random() * feeds.length)];
   return (currentFeed == newFeed ? randomFeed(feeds, currentFeed) : newFeed);
}

Or (thinking cap on…), you could filter out the current feed first which would take out the recursion and ensure only one call.

function randomFeed(feeds, currentFeed) {
   var filteredFeeds = feeds.filter(feed => feed != currentFeed);
   return filteredFeeds[Math.floor(Math.random() * filteredFeeds.length)];
}

words.filter(word => word != thisWord);
1 Like

How would I pass the current feed to the call?

I was going based off this. You must know it’s the current feed somehow since you’re asking how to not return it…

The only way I know what the current feed is, is what is showing on the page. How would I store that info in a javascript variable or some kind of marker that I can look at the next time the news is refreshed to see if the random pick is the same feed as what is currently displayed?

Starting with the flawed logic path.

<script>
var feeds = [
'http%3A%2F%2Ffeeds.skynews.com%2Ffeeds%2Frss%2Fus.xml',
'http%3A%2F%2Ffeeds.bbci.co.uk%2Fnews%2Fworld%2Frss.xml',
'https%3A%2F%2Fwww.nytimes.com%2Fsvc%2Fcollections%2Fv1%2Fpublish%2Fhttps%3A%2F%2Fwww.nytimes.com%2Fsection%2Fus%2Frss.xml',
'https%3A%2F%2Fwww.yahoo.com%2Fnews%2Frss',
'http%3A%2F%2Ffeeds.reuters.com%2FReuters%2FdomesticNews',
'http%3A%2F%2Ffeeds.washingtonpost.com%2Frss%2Frss_blogpost'
];

function randomFeed() {
   while((ret = Math.floor(Math.random() * feeds.length)) == feed) {}
   return ret;
}

var feed = randomFeed(feeds);

var link = "<script language='JavaScript' src='http://feedroll.com/rssviewer/feed2js.php?src=" + feeds[feed] + "&chan=title&num=4&desc=1&targ=y&utf=y&html=y' charset='UTF-8' type='text/javascript'><\/script>";

</script>

(You will probably need to instantiate feed before calling it, though)

1 Like

After some more research using Google, here is what I came up with. It seems to work.

<script>

var feeds = [
'http%3A%2F%2Ffeeds.skynews.com%2Ffeeds%2Frss%2Fus.xml',
'http%3A%2F%2Ffeeds.bbci.co.uk%2Fnews%2Fworld%2Frss.xml',
'https%3A%2F%2Fwww.nytimes.com%2Fsvc%2Fcollections%2Fv1%2Fpublish%2Fhttps%3A%2F%2Fwww.nytimes.com%2Fsection%2Fus%2Frss.xml',
'https%3A%2F%2Fwww.yahoo.com%2Fnews%2Frss',
'http%3A%2F%2Ffeeds.reuters.com%2FReuters%2FdomesticNews',
'http%3A%2F%2Ffeeds.washingtonpost.com%2Frss%2Frss_blogpost'
];

function randomFeed(feeds) {
   return feeds[Math.floor(Math.random() * feeds.length)];

}

var vOneLS = localStorage.getItem("vOneLocalStorage");

var feed = randomFeed(feeds);
var currentFeed = feed;

if (vOneLS == currentFeed) {
var feed = randomFeed(feeds);
}

localStorage.setItem("vOneLocalStorage", currentFeed);

var link = "<script language='JavaScript' src='http://feedroll.com/rssviewer/feed2js.php?src=" + feed + "&chan=title&num=4&desc=1&targ=y&utf=y&html=y' charset='UTF-8' type='text/javascript'><\/script>";

</script>