Can a function return another function?

As I delve into my Javascript learning journey, I encountered a puzzling Sa() function within this code snippet.

Initially, I deemed it to be an unsolvable problem like the Kobayashi Maru, but upon closer inspection, I suspect that this Sa() function might actually return not only other functions but also itself. Am I on the right track?

If indeed this hypothesis holds true, what purpose would such functionality serve?

var Jj, Ss, Sa;

Ss = function(n) {
  return n + 1;
};

Jj = function(f, n) {
  return function(i) {
  if (n < 1) {
  return i;
} else {
  return f(Jj(f, n - 1)(i));
  }
 };
};

Sa = function(a, b) {
  return Jj(Jj(Ss, a), b)(0);
};

Answer №1

Upon closer examination by @Bamar and myself, it appears that there is a mistake in the original question. The Jj(Sa, a) should actually be Jj(Ss, a).

After making this correction, the Sa function now provides the product of a and b, although it does so in a rather convoluted manner.

Below is an updated version of the (fixed) code with state information being logged:

var Jj, Ss, Sa;

Ss = function(n) {
  console.log("C:" + (n + 1)); 
  return n + 1;
};

Jj = function(f, nIterations) {
  return function(i) {
    if (nIterations < 1) {
      console.log('A' + nIterations + ":" + i); 
      return i;
    } else {
      console.log('B' + nIterations); 
      return f(Jj(f, nIterations - 1)(i));
    }
  };
};

Sa = function(a, b) {
  return Jj(Jj(Ss, a), b)(0);
};

Here is the result of running a sample test case:

> Sa(3,4)
B4
B3
B2
B1
A0:0
B3
B2
B1
A0:0
C:1
C:2
C:3
B3
B2
B1
A0:3
C:4
C:5
C:6
B3
B2
B1
A0:6
C:7
C:8
C:9
B3
B2
B1
A0:9
C:10
C:11
C:12
12

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's with the lack of acknowledgment when I triumph in rock, paper, scissors, lizard, spock?

My game is running smoothly, except for when lizard or spock are involved and I win. For some reason, the outcome does not display correctly, even though it works fine when I lose. I've double-checked for typos but couldn't find any. If someone c ...

Enhancing Accessibility of the 'Return to Top' Link

Currently, I am designing a web page that requires users to scroll extensively. To enhance the user experience, I have included a back-to-top link at the bottom of the page for easy navigation back to the top. This is the HTML markup I have implemented: ...

ReactJS issue: I am unable to display the dropdown menu while looping through an array

Although the title may be confusing, I am facing an issue in my Laravel project that integrates ReactJS on the front end. I am attempting to create a dropdown menu for users to select the status of a project. The statuses and their IDs are retrieved from a ...

Leveraging jQuery within a webpack module shared across multiple components, located outside the webpack root directory

When working with multiple layouts that rely on shared typescript files, it is important to ensure these files are accessible across different layouts using webpack. While attempting to include jquery in my ajax.ts, I encountered the following error: ERR ...

Error message: "Module not found" encountered while executing test case

I am a beginner in node and nightwatch and I have followed all the initial instructions from setting up node, npm, selenium standalone, starting the selenium driver. I also downloaded the chrome driver and placed it in the same directory. I created the con ...

Closing a webview completely in React JS

I am currently developing a website using React JS that will be integrated with an Android/iOS application and function like a webview. Within the website, I have implemented a cross button. When this button is clicked, I need to trigger a native bridge ...

require assistance in comprehending a php function

Seeking assistance in understanding why this issue persists. I have a text that is only visible starting from a specific date and time and gets removed at the end date and time. This code I am using is functioning properly. The text remains visible for a ...

Display the file name of the following/preceding images on the backward/forward button

Hello, I am fairly new to web development and could use some professional assistance. I have a slideshow set up, but I am struggling with adding the filename of the next or previous images to the back/forward icons of the slideshow. Is this achievable with ...

Retrieve the file path from a JavaScript file

Currently, I am facing an issue where I have a list that needs to be populated with file images and hrefs. While I can retrieve the content inside a directory, I am unable to access the path. This is causing problems as images and hrefs are not functioning ...

Refreshing a div by replacing its content with text entered into an input field

I'm exploring React and attempting to create a basic div that dynamically changes its text based on input from an input box. Below is the code I have written so far: import React from "react"; import ReactDOM from "react-dom"; const rootElement = d ...

Determine changes in data retrieved from a JSON file using React

I've been working on a cryptocurrency app using React and a JSON API to fetch the latest data. My approach involves using fetch to load the JSON API and setInterval to refresh the app every 10 seconds. Now, I'm wondering if there's a way to ...

Leveraging bootstrap's grid system to prioritize content layout based on the

Currently working on a compact React Js project with Bootstrap for styling. One obstacle I've encountered is the need to display the last element as the first one on the page. To achieve this, I plan to utilize the order-1 and order-12 classes from Bo ...

Searching for text using JQuery autocomplete feature with results fetched from an external source

I am currently working on integrating an input field with autocomplete functionality using the Google Books API to suggest book titles based on user input. My framework of choice for this implementation is Django. So far, I have managed to achieve the fol ...

Identify when 2 sets of radio buttons are chosen using jQuery

I need assistance with a webpage that presents the user with two simple yes-no inquiries. Below each question, there are two radio buttons for selecting either yes or no. <p>Question 1: Yes or No?</p> <input type="radio" name="q ...

Vue.js powered search bar for exploring countries worldwide (utilizing the restcountries API)

Despite successfully loading the API country data (as evidenced by the console.log() entry and the accompanying picture of my work desk), the country information does not display when hovering the mouse cursor over the search bar field (expecting a dropdow ...

A step-by-step guide on recursively removing all empty array children [] from a nested JSON structure

When I receive a JSON response, I utilize nested JSON data from my GeoRegionCountries APIController and a custom class TreeView to structure the data for a plugin. The specific plugin I am using is a combo multi-select Treeview, accessible through this lin ...

Tips on passing a variable and API response to the following promise in JavaScript!

Using the initial promise "crypto.model.find()" allows me to store an array of "symbols" ( symbol[] ) from the database and retrieve some IDs that I will utilize to construct a URL for making a request to an API using axios.get(url). Upon receiving a resp ...

Express.js session management -- Ensuring sessions are created exclusively for authenticated users

I've implemented the express-session Node module to handle secure session cookies. // To ensure certain requests require an active session, this code sets up a secure session cookie app.use(expressSession({ secret: "wouldn'tyouliketoknow", ...

Attempted to fetch information using Ajax in Grails

I am attempting to utilize jQuery and ajax with the Grails framework to load the content registration page. However, upon loading the page, I am encountering an issue where the menu and registration fields are duplicated within the page. <script src ...

`javascript pop up notification will only display one time`

I am currently developing a chrome extension with two different modes. When the user clicks on the icon, a pop-up message should appear to indicate which mode is active. However, I am facing an issue where the message does not always display when the pop- ...