Creating patterns in Angular - Struggling to achieve the expected result

I am encountering a particular issue that I have been attempting to resolve through various methods. Unfortunately, despite my efforts, the current output is not aligning with what I desire. I would greatly appreciate it if someone could review my process and point out where I may be going wrong in order to achieve the desired outcome. I am working with an input number(n), which should fall within the range of 2 to 10. The task at hand involves validating whether the input value is outside of the expected range.

The output I am currently receiving is as follows:

If I enter n = 2:

A

B C

If I enter n = 3:

A

B C 

D E F

However, the outputs that I am aiming for are as follows:

Example 1. If I input n = 3, the corresponding output should look like this:

A

A B

C D E

Example 2: If I input n = 4, the resulting output should be displayed as follows:

A

A B

A B C

D E F G

Access the Stackblitz here.

Answer №1

To modify the logic, follow these steps.

Start by breaking down the string into an array.

Next, utilize the slice function to set the start and end points.

Adjust the start and end positions to achieve the desired result.

For the last index specifically, extract from the final element to n characters, this will yield the expected outcome.

generateSeries(n: number): string[] {
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  let series: string[] = [];
  let currentCharIndex = 0;

  for (let j = 1; j <= n; j++) {
    const alphaArray = alphabet.split('');
    const outputResult = j !== n ? alphaArray.slice(0, j) : alphaArray.slice(j - 1, j + n -1);
    console.log(outputResult);
    series.push(outputResult.join(' '));
  }

  return series;
}

Try Out Stackblitz Demo

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

Modify the initial letter of the user input as it is entered using JQuery

I've been experimenting with auto-capitalizing the first character that a user inputs in a textarea or input field. My initial approach was as follows: $(document).ready(function() { $('input').on('keydown', function() { if ( ...

Can we determine which formControl value has been altered?

How can we determine which specific formControl had its value modified when monitoring changes in a entire form, rather than focusing on a particular control? The code in question looks like this: myForm = fb.group( .... ); ... myForm.valueChanges.subsc ...

Ways to retrieve the most recent value from an Angular 4 subscription

What is the best way to retrieve the most recent value from a subscribe method in Angular 4? this.AbcSrvice.value.subscribe(data => {console.log(data)}) ...

The effectiveness of the module is not meeting the anticipated standards

I successfully integrated a good plugin into my hapi server, and all responses are being logged. However, when I use console.log, console.error, console.warn, and console.info, the logs are only printing as plain text instead of using the good plugin forma ...

The issue of basic authentication failing to function on Internet Explorer and Chrome, yet operating successfully on Firefox

Here is how my authentication code looks: public class BasicAuthenticationMessageHandler : DelegatingHandler { private const string BasicAuthResponseHeader = "WWW-Authenticate"; private const string BasicAuthResponseHeaderValue = "Basi ...

JQuery ajax DELETE request encounters issues during the OPTIONS stage

I'm encountering an issue while trying to send a delete request using JQuery as it consistently results in a 500 internal server error. Here's the code snippet I'm using for the request: $('#deleteReview').click(function(event, ui ...

Reorganize child JSON objects into a new object that includes a parent ID

Exploring the realm of JavaScript, I am currently delving into Node.JS to interact with an API and save the data in a SQL Server. Utilizing the "request" and "mssql" Node packages for this task as they possess robust documentation and support. My query re ...

The HTTPOnly cookie is not accessible within the getServerSideProps function in Next.js

Why isn't the jid cookie available in getServerSideProps using Next JS? Here's the scenario: https://i.stack.imgur.com/FLRGk.png The jid cookie is generated by an Expressjs API at https://api-dev.example.com. I'm attempting to retrieve thi ...

Is it necessary for JavaScript functions to be stored in a separate file in order to be invoked?

Here is the scenario that unfolded: Within a .php file, I had the following form: <form action="/flash_card/scripts/create_user_gate.php" onsubmit="return validateForm()" method="post"> <table align="center"> ...

Unable to add new Instance Properties in Vue.js within a Laravel project

I am attempting to develop a localization property similar to __('text') in Laravel blade template. I have set up a global window variable that contains all required data under the name window.i18n Below is my resourses/js/app.js file: require(& ...

extracting an attribute from a class's search functionality

Let's consider the following HTML structure: <div id="container"> <div> <div class="main" data-id="thisID"> </div> </div> </div> If I want to retrieve the value inside the data-id attribute ...

I'm having trouble executing jest in my typescript-vue project

https://i.sstatic.net/kKqKy.png package.json { "name": "application", "version": "0.1.0", "private": true, "scripts": { "serve": "vue-cli-service serve", "build": "vue-cli-service build ", "lint": "vue-cli-service lint", "dev": "v ...

What is the TypeScript syntax for indicating multiple generic types for a variable?

Currently working on transitioning one of my projects from JavaScript to TypeScript, however I've hit a roadblock when it comes to type annotation. I have an interface called Serializer and a class that merges these interfaces as shown below: interfa ...

Resolving the issue of 'NotificationKind' not being found in the 'rxjs' export

NotificationKind has been marked as deprecated. More information can be found here. However, when trying to build an Angular 8 app, the following error is encountered: ng build Date: 2019-06-08T16:44:40.118Z Hash: 7f30f6f31d45ce95291b Time: 12019ms chunk ...

Incorporating fresh components and newly defined attributes in Angular

Is there a way for me to click on a new component button, specify a name, description, select type, and add attributes such as default value and type? I need all this information to be saved and for the new button to appear in the drag-and-drop section. ...

Can you trigger the playback of a sound file by clicking on a specific URL?

Can a sound file be played when clicking depending on the URL? For example: url/sample/#1 Click to play sound1 url/sample/#3 Click to play sound2 ...

Tips on avoiding updates to a defined object when a new object is filtered (created from the original object)

Is there a way to filter an array of objects based on their year without altering the original object? Whenever I apply a filter, it affects both the newly created object and the original one. However, I need the original object to remain unchanged so that ...

Deciphering the JavaScript code excerpt

This information was sourced from (function ($, undefined) { // more code ... $.getJSON("https://api.github.com/orgs/twitter/members?callback=?", function (result) { var members = result.data; $(function () { $("#num- ...

adding content to div is becoming less speedy

Currently, I'm developing a drawing board using only html/css/jquery and the drawing functionality is working smoothly. The approach I've taken involves capturing the mousemove events and placing a dot (div) at each point where the event occurs, ...

Variable Returned by AJAX

I am attempting to send a variable using a select box via POST method, then use AJAX to submit the data and finally leverage that variable on the original page to update SQL queries. Below is the code snippet I currently have: <script type="text/j ...