Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6701627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:57:24+00:00 2026-05-26T06:57:24+00:00

My professor says I have a logic error in the greetUser function of code

  • 0

My professor says I have a logic error in the greetUser function of code I was already graded on. The code is based on the example in the text, so I’m stunned as to determining the logic. This was her message to me:

The greetUser function is not reading the cookie correctly for the returning visitor.
Check this line of code in the readCookie function – it has a logic error.

This is the readCookie function:

function readCookie(name) {
    var nameEQ = name + "=";
    var x = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = cookies[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

This is the greetUser function:

function greetUser(){
    userName = readCookie("rock_username");
    if(userName)
        alert("Hello " + userName + ", I missed you.");
    else 
        alert("Hello, I am your pet rock");
}

From my novice eyes it all looks logical to me, the same as it was in the text example.
Any suggestions

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:57:25+00:00Added an answer on May 26, 2026 at 6:57 am
    var x = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
    var c = cookies[i];
    

    You are iterating on “ca” (what is that?) instead of “x”, and then you are reading from the cookies array? 3 different variables, same meaning?

    I’ll explain: var x = document.cookie.split(';'); splits the string stored in document.cookie into an array, that will effectively be held inside the x parameter.

    Then, inside the for() construct, you are increasing i up until the amount of elements found inside the array you just created from splitting the string. That way, each time you do c = x[i]; (correct version) you will get the next portion of the document.cookie string that you split with the ‘;’ character.

    This allows you to work on all the interesting parts of document.cookie, which are, by definition, the user agent (browser) cookies. In your example, you are iterating over the cookies to find a specific one – the ‘name’ cookie – in order to print it as a greet to the user.

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have downloaded this code; http://www.cs.sfu.ca/%7Emori/research/superpixels/superpixels64.tar.gz In the readme file, it says; Run mex
I have been taking a few graduate classes with a professor I like alot
So I'm working on rewriting a program for a professor, and I have some
I'm working on a homework assignment to modify code given by my professor using
Say if I have a processor like this which says # cores = 4,
I have to code a desktop application and some dynamic web content. I'm planning
I have the following code public static int unknown(String x) { if ((x.length()==1) &&
I have been reading an article about Lockless Programming in MSDN. It says :
In my parallel programming book, I came across this code that says the slaves
I have been challenged by a professor to develop a little Bluetooth Demo app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.