0

This is the start of a long project I am working on. I have an array of articles that are selected at random to be evaluated in a library instruction setting, and based on which article is selected I want a certain alert dialog box to display. To do this, I am thinking I need to have a local variable for one function passed to another function. I have tried the first solution listed in the following thread with no luck: Passing a local variable from one function to another

Any help will be appreciated.

`

var naturalNewsArticle = "https://www.naturalnews.com/2023-02-20-immortalized-cell-lines-lab-grown-meat-cancer.html";
var cbsArticle = "https://www.cbsnews.com/news/putin-ukraine-war-speech-today-blames-us-nato-after-one-year-invasion/";
var msnbcArticle = "https://www.msnbc.com/rachel-maddow-show/maddowblog/buttigieg-reminds-rubio-recent-record-rail-inspections-rcna71584";
var reutersArticle = "https://www.reuters.com/legal/government/san-jose-asks-judge-toss-challenge-gun-insurance-law-2023-02-17/";

// Arrary of articles for the function to randomly choose from
var articles = [naturalNewsArticle, cbsArticle, msnbcArticle, reutersArticle]


function randomize()
    {
        // Dislay iframe element
        var displayFrame = document.getElementById("articleFrame");
        displayFrame.style.display = "block";
        
        // Display form
        var displayForm = document.getElementById("assessment");
        displayForm.style.display = "block";
        
        if(articles.length==0)
        {
            // Once all the articles have been cycled through randomly, reload the page
            location.reload();
            
        }
        var randArticle = Math.floor(Math.random()*articles.length); // select an index randomly based on the number of remaining available articles
        var selectedArticle = articles[randArticle];
        articles.splice(randArticle,1); // remove the article selected so it can't be selected next time
        document.getElementById("articleFrame").src = selectedArticle;        
        
        submitAns(selectedArticle);
    }

// Currently in testing stage
function submitAns(val) 

    {            
        var targetArticle = val;
        if (targetArticle = naturalNewsArticlef) {
            alert("Test 1");
        } else if (targetArticle = cbsArticle) {
            alert("Test 2");
        } else if (targetArticle = msnbcArticle) {
            alert("Test 3");
        } else if (targetArticle = reutersArticle) {
            alert("Test 4");
        }
    }

`

Irene
  • 3
  • 4

1 Answers1

1

There are two problems in your code:

  1. There is a typo: naturalNewsArticlef should be naturalNewsArticle

  2. You want to compare (===) and not assign (=) here:

    if (targetArticle === naturalNewsArticle) {
      alert("Test 1");
    } else if (targetArticle === cbsArticle) {
      alert("Test 2");
    } else if (targetArticle === msnbcArticle) {
      alert("Test 3");
    } else if (targetArticle === reutersArticle) {
      alert("Test 4");
    }
    

otherwise your output is always "Test 1" since targetArticle will always be naturalNewsArticle.

EDIT: Here is a working demo using your code:

var naturalNewsArticle = "https://www.naturalnews.com/2023-02-20-immortalized-cell-lines-lab-grown-meat-cancer.html";
var cbsArticle = "https://www.cbsnews.com/news/putin-ukraine-war-speech-today-blames-us-nato-after-one-year-invasion/";
var msnbcArticle = "https://www.msnbc.com/rachel-maddow-show/maddowblog/buttigieg-reminds-rubio-recent-record-rail-inspections-rcna71584";
var reutersArticle = "https://www.reuters.com/legal/government/san-jose-asks-judge-toss-challenge-gun-insurance-law-2023-02-17/";

// Arrary of articles for the function to randomly choose from
var articles = [naturalNewsArticle, cbsArticle, msnbcArticle, reutersArticle]


function randomize() {
  // Dislay iframe element
  var displayFrame = document.getElementById("articleFrame");
  displayFrame.style.display = "block";

  // Display form
  var displayForm = document.getElementById("assessment");
  displayForm.style.display = "block";

  if (articles.length == 0) {
    // Once all the articles have been cycled through randomly, reload the page
    alert('reloading...');
    location.reload();
    return;
  }
  var randArticle = Math.floor(Math.random() * articles.length); // select an index randomly based on the number of remaining available articles
  var selectedArticle = articles[randArticle];
  articles.splice(randArticle, 1); // remove the article selected so it can't be selected next time
  document.getElementById("articleFrame").src = selectedArticle;

  submitAns(selectedArticle);
}

// Currently in testing stage
function submitAns(val) {
  var targetArticle = val;
  if (targetArticle === naturalNewsArticle) {
    alert("Test 1");
  } else if (targetArticle === cbsArticle) {
    alert("Test 2");
  } else if (targetArticle === msnbcArticle) {
    alert("Test 3");
  } else if (targetArticle === reutersArticle) {
    alert("Test 4");
  }
}
<div id="articleFrame" style="display:none;">article frame</div>
<div id="assessment" style="display:none;">assessment</div>

<button type="button" onclick="randomize();">randomize</button>
Đinh Carabus
  • 3,403
  • 4
  • 22
  • 44
  • Ok I have fixed that issue. Thank you. However, I am still not receiving any output? Could it be an issue with the HTML code? – Irene Feb 25 '23 at 21:06
  • @Irene, I've added a working demo. Maybe it will help you to find the error in your code. – Đinh Carabus Feb 25 '23 at 21:36