Function producing undefined variable

Course Queries Syllabus Queries 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I am working within a Learning Management System and creating some custom components

This is returning the url but the "course_syllabus_code" shows up as undefined after the url. I am a beginner to Javascript and have looked at some of the other answers on Google but still cannot figure it out.

Basically the user will be able to open the code and enter the Variables(There will be multiple) at the top. Then further below is where all the magic is supposed to happen...

The structure of the variable being entered cannot change but maybe the "magic" isn't being executed in the best way possible.

 type="text/javascript">

var course_syllabus_code = "72524";

function syllabusLink(course_syllabus_code) {
location.href = 'https://foo.com'+course_syllabus_code;
}


  • href="javascript:syllabusLink()"> src="CourseButton3.png" />Syllabus
profilepic.png
manpreet 2 years ago

You're passing course_syllabus_code as an argument to the function, but calling the function without any arguments. When you call a function with fewer arguments than it defines, the ones that you don't pass come through with the value undefined. Since you've given the function argument the same name as the global, it "shadows" (obscures, hides) the global from the code in the function, which will access the argument instead.

You have three choices (only do one of these, not all of them):

  1. Use the global by just removing the declared function argument:

    function syllabusLink() {
    // Removed here ------^
  2. Or, pass the global into the function where you call it:

     href="javascript:syllabusLink(course_syllabus_code)"> src="CourseButton3.png" />Syllabus

        I'd probably change the name of the argument as well (here I've used scode):

    function syllabusLink(scode) {
        location.href = 'https://foo.com'+scode;
    }
  3. Or, don't make course_syllabus_code a global at all, and pass it to the function:

     href="javascript:syllabusLink('72524')"> src="CourseButton3.png" />Syllabus

I'd probably go for #3, unless there's a really good reason it has to be a global variable.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community