The program is only displaying a portion of the linked list, rather than showing the complete list

Currently, I am working on converting arrays into linked lists and vice versa. The challenge I am facing is that when I try to log the linked list I have created, it shows incorrect values. However, when I convert the same linked list back into an array, the console log displays the correct values.

For example, in the code snippet below, when I use listToArray() function, I correctly get the output [1, 2, 3, 4, 5]. But when I use arrayToList() function, the output is { value: 1, rest: { value: 2, rest: { value: 3, rest: [Object] } } }. This makes me wonder what happened to the values 4 and 5. Given that listToArray() uses the same list generated from arrayToList(), shouldn't all values be intact?

var array = [1, 2, 3, 4, 5];

var arrayToList = function(arr) {
  var list = {};
  var head = list;

  for (i = 0; i < arr.length; i += 1) {
    list.value = arr[i];
    list.rest = {};

    if (i == arr.length - 1) {
      list = null;
    } else {
      list = list.rest;
    }
  }

  return head;
}

var listToArray = function(list) {
  var arr = [];
  var index = 0;

  while(list.rest != null) {
    arr[index] = list.value;
    list = list.rest;
    index += 1;
  }

  return arr;
}

console.log(listToArray(arrayToList(array)));
console.log(arrayToList(array));

Answer №1

Are you executing code in a browser or using Node.js? The console behavior varies between the two environments. In Node, when you use console.log(someObject), it will invoke util.inspect to convert your Object into a String. By default, the recursion depth of util.inspect is set to 2; any further levels will display as [Object].

You can experiment with the following:

console.log(util.inspect({depth: null}));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to deactivate ng-repeat using a button in AngularJS?

How can I disable the apply button once it has been clicked by the user in a list of stores based on services? I am using ng-repeat to display the listing. Additionally, how can I write an apply function to disable a particular service once it has been app ...

Incorporate keys and values into a multidimensional array depending on specified criteria

I am curious if it's feasible to insert a key and value into an array based on specific conditions. This code fetches sports results via an api call for multiple teams using their teamID numbers. $length = $numberofTeams for ($i = 0; $i < $length; ...

Is a Login interface available, integrating JQuery, Bootstrap, Ajax, or ColdFusion 16?

I am currently in the process of transitioning one of my applications from an old system to a new, separate system. The Single Page App is currently built using JQuery/HTML5/CSS/AJAX on the front end and ColdFusion on the back end. Before I dive into devel ...

jQuery can bring life to pseudo elements through animation

Currently, I am utilizing a:after, and it includes right: Ypx. I am interested in employing the animate method within jQuery to transition the element (a:after) from Ypx to Zpx. Is there a way to achieve this? Here is an example: [link-text][after] [lin ...

Is there a way to utilize classes in various elements when other jQuery selectors are not functioning properly?

Could someone please clarify or provide an alternative solution to the following situation: The class fruit is present in two different tag elements, and one of these elements has the add class used in a jQuery selector. However, the alert box is not disp ...

Printing an Iframe using Safari print with Javascript results in an empty output

After researching the issue of printing blanks in Safari, I discovered that a white flash occurs before the print dialog can grab the content of the iframe. All my javascript works perfectly in every browser except for Safari. When attempting to print, it ...

Resizing nested elements while maintaining consistent padding dimensions

If you're looking to create a sleek foundation for a 200px wide, 30px high editable combobox that can be easily used with angular binding or other JavaScript data-binding solutions, consider the following HTML code. However, there's a desire to m ...

What is the best method for efficiently converting a binary string to a binary byte array in Java?

Given a binary string String a = "100110", I am looking to convert it into a binary byte array byte[] b = {1,0,0,1,1,0}. Currently, I am using the following method: for (int i=0; i<a.length; i++) { b[i]= Byte.parseByte(a.substring(i, i+1)); } Howe ...

Tips for creating a fixed footer that adjusts with a flexible wrapper

Feeling incredibly stressed, I embarked on a quest to find the perfect sticky footer. After searching Google for what seemed like an eternity, I came across multiple tutorials recommending the use of min-height:100%. However, this approach caused the wrap ...

"Trouble ensues when OrbitControls fail to function properly while loading a

I have a code snippet that displays a 3D file (obj, stl, 3mf) uploaded by the user using three.js OBJLoader, STLLoader, and 3MFLoader. Everything seems to be working fine, but I attempted to integrate OrbitControls so users can zoom in, zoom out, rotate, e ...

Utilizing inline keyboard markup callbacks for sending messages to channels via a Telegram bot

When utilizing my Telegram bot to send messages to a channel, I also want to include an inline keyboard with each message. The layout of the keyboard should resemble this: inline message keyboard The challenge I am currently facing is figuring out how to ...

Ways to eliminate the absence of the 'Access-Control-Allow-Origin' header in an error message when using a Java-based web-service server

I'm currently working on developing a webservice using Java as the server and Javascript as the client. My goal is to send a Post request with JSON data and receive a Post response with JSON data from the server. However, since the client and server h ...

The body request has been divided into various logs

I am currently working on a JavaScript project running with GCP app engine, and I have encountered an issue with the req.body of a webhook request. To check the logs, I am using the gcloud app logs tail -s command. When I run console.log(req.body), the ou ...

Trouble embedding iframes in local files?

I have recently created a file which includes the following code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8> <style> iframe { height: 500px; width: 600px; } ...

What is the best way to implement ng-change within an ng-repeat?

My goal is to trigger a function every time a check box is clicked in ng-change. I am looking for a way to determine the value of the model or identify which checkbox was clicked, so that I can call another function based on the checked value. Additionally ...

Tips for selecting a specific range of values in an array post sorting

After spending countless days working on this program, I am still facing several challenges. The task at hand is to develop a program capable of sorting using Bubble Sort, Selection Sort, Insertion Sort, and Shell Sort algorithms. The program should read ...

Are there any React-UI libraries that come with a stylish green color scheme?

Currently working on a small React site and deciding on a UI library. While I do like Material UI and Bootstrap, I find that they have limited default themes available. I am looking for libraries with green-color-based themes or ones that make it easy to ...

The issue with Grid component in Nested Single file Component

I'm currently working on creating a grid component in Vue to set up a sortable and searchable chart using Single File Component. I've also integrated vue-router into the project. Below are my two .vue files. At the moment, I can only see the sear ...

Having trouble with Selenium WebDriverJS on both FireFox and Internet Explorer

Having developed multiple JavaScript tests using chromedriver to run them in Chrome, I am now facing the challenge of running these same tests in FireFox and IE. The test below is functional in Chrome: var assert = require('assert'), test = requ ...

Straying from guidelines, enjoying while learning Structured workout

As I prepare for my final exam, I can't help but feel that I might be dedicating too much time to a particular topic. Seeking some feedback on this matter, I stumbled upon the following question from the book "Beginning C, from novice to professional" ...