Maximize Rotation - JavaScript Rotation

Currently tackling a Codewars challenge.

The task involves finding the maximum possible result after performing a rotation on a given number.

This rotation is unique in that 'n' number of digits will remain fixed after each rotation, with 'n' increasing as more rotations are performed.

For instance, when starting with the number 56789:

67895 (6 remains static, while 7 is rotated to the end)

68957 (6 and 8 remain stable, while 9 is moved to the back)

68579 (6, 8, and 5 stay unchanged, while 7 is shifted to the end)

68597 (6, 8, 5, and 9 do not move - no further rotations can occur)

The goal is to return the highest value achieved from these rotations - 68957.

Below is my current implementation:

function maxRot(n) {
  let listOfNums = [];
  let array = Array.from(n.toString());
  let num = 0;
  while (num < array.length -1) {
    let number = array.splice(num, 1);
    array.push(Number(number));
    listOfNums.push(Number(array.join("")));
    num++;
  }
  listOfNums.sort((a, b) => b - a);
  return listOfNums[0];
}
console.log(maxRot(56789));

However, this solution is failing roughly half of the tests on Codewars platform.

The approach involves removing one digit at a time and adding it to the end of the array, then storing the updated array in a listOfNums array. Finally, sorting this array in descending order and returning the first element.

I am unsure about what else could be attempted to improve the results.

Once again, the link to the Codewars challenge can be found here.

Answer №1

As pointed out by @georg, I inadvertently omitted re-adding the original number back to the list.

function maxRot(n) {
  let listOfNumbers = [];
  let array = Array.from(n.toString());
  let index = 0;
  while (index < array.length -1) {
    let number = array.splice(index, 1);
    array.push(Number(number));
    listOfNumbers.push(Number(array.join("")));
    index++;
  }
  listOfNumbers.unshift(n);
  listOfNumbers.sort((a, b) => b - a);
  return listOfNumbers[0];
}

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

Ruby: Evaluating the Similarities Between Two Arrays Containing Hash Data

As a newbie to Ruby (using version 1.9.1), I've been learning mostly from Google search results. Currently, I'm facing an issue with comparing two arrays of hashes which is taking too long due to the large sizes and almost running out of memory. ...

What is the most effective method for sorting through vast amounts of data with Javascript, React, and Redux?

Currently, I am working with a minimum JSON data set of 90k [...] and utilizing the .filter method. Everything is functioning correctly without any issues, however, from a performance standpoint, I am curious about potential improvements. Any suggestions o ...

Retrieving the checkbox value from a dropdown selection

I'm stuck and feeling lost here - I must be missing something obvious. Any help will be greatly appreciated! (I am a beginner in html and javascript) I have created a dropdown menu with an unordered list of items populated from JSON data. Here is the ...

Tips for transforming tabular text into JSON format with nodejs?

I am currently working with node.js and looking to transform the provided string: 13 0.02 23 0.11 0.15 37 AIRLINES 74 0.02 343 0.08 0.23 708 ALL CLOTHING STORES 211 0.02 127 0.02 0.03 386 ...

Browser Compatibility in Three.js

Could someone assist me with activating webgl on Internet Explorer version 8? I am able to use Firefox and Chrome without any issues. Your help would be greatly appreciated. ...

Calculating the subtotal amount using Vue.js is a straightforward process

My Vue.js project functions as a shopping cart, and I am looking to calculate the subtotal for each product row. Here is an excerpt of my HTML code: <div id="app"> <table border="1"> <thead> <tr> <th>#</th ...

Does the gltf loader in three.js have compatibility issues with Internet Explorer 11?

Despite the claims on the official website that gltf files should load in three.js scenes using IE11, I have been experiencing issues with the loader. Even the examples provided by three.js fail to work on Internet Explorer when it comes to loading gltf fi ...

Designing websites using elements that float to the right in a responsive manner

Responsive design often uses percentage width and absolute positioning to adjust to different screen sizes on various devices. What if we explore the use of the float right CSS style, which is not as commonly used but offers high cross-browser compatibilit ...

I'm looking to center the column content vertically - any tips on how to do this using Bootstrap?

Hello! I am looking to vertically align the content of this column in the center. Here is an image of my form: https://i.stack.imgur.com/nzmdh.png Below is the corresponding code: <div class="row"> <div class="form-group col-lg-2"> ...

Can you explain the significance of the <%= %> HTML tag?

I've recently been tackling a project with Webpack. For those not familiar, Webpack is a bundler that combines all your files into one final product. One task I encountered was trying to insert one HTML file into another, similar to an import or requ ...

EaselJS - Utilizing multiple canvases with varying frame rates

Currently, I am exploring EaselJS by creating two animation instances using sprite sheets on two separate canvases positioned at different locations but with the same z-index. The issue I am facing is that these instances are not layered properly. My setup ...

Extracting the color of a text layer with PSD.JS

Currently, I am utilizing PSD.JS (an efficient photoshop PSD file parser designed for both NodeJS and browsers) to decode several PSD files. Upon extraction of information from a text layer by the parser, color data is returned in the form of an array. Fo ...

Tips for determining whether the current request is an AJAX request in MVC3 view using JavaScript

I have a div with a maximum height and overflow set to auto. When the size of the div overflows, a scroll bar is automatically added. However, I have also set $("#itemsdiv").scrollTop(10000); so that the scroll bar is always at the bottom. Now, I want t ...

Pagination of AJAX result on the client side using jQuery

My issue revolves around pagination in AJAX. I want to avoid overwhelming my search result page with a long list of returned HTML, so I need to split it into smaller sections. The Ajax code I am currently using is: $.ajax({ url: "getflightlist.php?pa ...

Error Encountered: POST method not supported in ajax request using djangoIs this a

I am currently encountering an issue while trying to pass form data values through ajax. I keep getting a method not allowed error when attempting to add a comment on a blog post. The form below is located inside the blog_detail page: <form id="co ...

Tips for breaking apart elements of a JavaScript array when a specific delimiter is present in an object property

I am facing a challenge with an array of objects where some properties contain commas. My goal is to split these properties into new objects and recursively copy the rest of the properties into new array elements. For example, consider this original array ...

Naming axes in Chart.js is a simple process that can easily be implemented

Greetings to all, I'm currently working on creating bar charts using chartjs...everything is going smoothly except for one thing - I am struggling to find a clean way to name my axes without resorting to CSS tricks like absolute positioning. Take a ...

Exploring the retrieved data from the AJAX/ASP.NET controller update for a fresh perspective

When selectbox is called, it triggers the 'getDepAndMan()' function. A value is extracted from the selectbox (successful). The function calls methods in the 'GetDepartmentAndManager' controller (successful). The controller returns ...

Making the toggle button functional on the navbar

Trying to customize my navbar for tablet and mobile devices is proving to be a challenge. I am looking for a way to make the dropdown button take up the entire page when clicked on. It seems like JavaScript might be the solution, but I'm not quite sur ...

preventing the ball from landing inside the container (JavaScript)

I developed a straightforward website with the following functionality: Upon entering any text into the prompt and clicking okay, a ball will drop into the 1st box, namely the Past Thoughts box. Below is the code snippet: HTML <h1> Welcome t ...