Question

In: Computer Science

Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function,...

Code programs using ReadlineSync for prompts.

Note: For all programs, create and call a main function, and if required additional functions called by the main. Also please use the ES6 style of keywords => instead of the older function and for local scope variables use the keyword let, not var

triangle.js

Write a program that is required to use nested loops to generate a triangle as shown in the sample run below. The program should begin by prompting the user to enter the number of lines in the triangle and output to the console to match the example below.

Hint: Each line will contain either "O" or " " (spaces) to align the output to appear right justified

How many lines of the triangle?:6

OOOOOO
OOOOO
OOOO
OOO
OO
O

Solutions

Expert Solution

//create a folder and open a command prompt in that location

//npm install readline-sync

//type the above command to install readline-syncc

//--------- triangle.js -------

//es6 style function

//store the function in printPattern to call.

printPattern = (num)=>

{

//using let instead of var.

//from i = num down to 0

for(let i = num;i>0;i--)

{

//line variable to store the result

//console.log() will print data in logs.

//we can't print the pattern side by side.

//so use a string variable to store the patter for that

//particular line.

let line = "";

//number of O's in line

//add the i number of O's to the line.

for(let j = 1;j<=i;j++)

{

//add O to the line.

line += "O";

}

//log the line.

console.log(line);

}

}


//main function.

main = ()=>

{

//declare readline

let readline = require("readline-sync");

//read input from user.

let num = readline.question("enter the number of lines in the triangle: ");

//print the pattern by calling the function.

printPattern(num);

}

//call the main function.

main();

//please like the answer.

/*

-------------- OUTPUT ------------

C:\Users\Triangle>node triangle.js

enter the number of lines in the triangle: 6

OOOOOO

OOOOO

OOOO

OOO

OO

O

C:\Users\Triangle>node triangle.js

enter the number of lines in the triangle: sd

C:\Users\Triangle>node triangle.js

enter the number of lines in the triangle: 7

OOOOOOO

OOOOOO

OOOOO

OOOO

OOO

OO

O

*/


Related Solutions

Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function,...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function, and if required additional functions called by the main. Also please use the ES6 style of keywords => instead of the older function and for local scope variables use the keyword let, not var Name: coinflip.js For this program you will have two functions, one called main and the second called flip. This program is also required the use of a loop construct. Write...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function,...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function, and if required additional functions called by the main. Also please use the ES6 style of keywords => instead of the older function and for local scope variables use the keyword let, not var Name: cookout.js Assume that hot dogs come in packages of 10, and hot dog buns come in packages of 8. Write a program called cookout.js, that calculates the number of...
JavaScript Programming Assignment PLEASE NOTE:  You must create and call a main function, and if instructed include...
JavaScript Programming Assignment PLEASE NOTE:  You must create and call a main function, and if instructed include additional functions called by the main. Make sure to use ES6 style of keywords => instead of the older function and for local scope variables use the keyword let and not a keyword var. Make sure to follow the requirements and recheck before submitting. PROJECT GOAL: Assume that hot dogs come in packages of 10, and hot dog buns come in packages of 8....
JavaScript Programming Assignment PLEASE NOTE:  You must create and call a main function, and if instructed include...
JavaScript Programming Assignment PLEASE NOTE:  You must create and call a main function, and if instructed include additional functions called by the main. Make sure to use ES6 style of keywords => instead of the older function and for local scope variables use the keyword let and not a keyword var. Make sure to follow the requirements and recheck before submitting. PROJECT GOAL: Create a program that simulates tossing a coin. This program should be titled flippingacoin.js and will require you...
Create all necessary code to make this main function work. It is not allowed to change...
Create all necessary code to make this main function work. It is not allowed to change the main function. int main() {        int ListDataSample1[] = { 1, 1, 1 };        int ListDataSample2[] = { 2, 2, 2 };        List<int> List1 = List<int>(ListDataSample2, 3);        List<int> List2 = List<int>(ListDataSample2, 3);               cout << "List1 :" << List1 << endl;        cout << "List2 :" << List2 << endl << endl;        List1 += List2;               cout...
Using C++ 1. Create a main function in a main.cpp file. The main function should look...
Using C++ 1. Create a main function in a main.cpp file. The main function should look as follows int main() {return 0;} 2. Create an array. 3. Ask user to enter numbers in size of your array. 4. Take the numbers and store them in your array. 5. Go through your array and add all the numbers. 6. Calculate the average of the numbers. 7. Display the numbers, sum and average.
This is my code for an array using the bubblesort in the function outside of main....
This is my code for an array using the bubblesort in the function outside of main. My bubblesort is not working correctly , and I was told i'm missing a +j instead of +i in the function for void sorter.Can someone please help me with making my sorter work properly? everything else is fine and runs great. Thank you #include <stdio.h> #include <stdlib.h> #include <time.h> void printer(int *ptr, int length); void randomFill(int *ptr, int length); void sorter(int *ptr, int length);...
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
You will be writing the function definition and the main function with the function call. Be...
You will be writing the function definition and the main function with the function call. Be sure to write the function definition below the main function. The function prototype has been provided, you may add the prototype to you answer. You do not need to add the preprocessor directives like #include<stdio.h>. Write the function definition for UpdateGrade . The function will take two arguments, a double called originalGrade a pointer to a double called newGradePtr. This function will have a...
Case-IT Auditing Code developers modify or create programs. The IT testing team performs all internal IT...
Case-IT Auditing Code developers modify or create programs. The IT testing team performs all internal IT testing; however, the business areas perform their own user acceptance testing. The IT Departments Middleware team is responsible for migrating all code to production (except for database triggers). The Middleware Team does not perform any code development activities. Although SQL database triggers are developed or modified by code developers, the migration for the triggers is performed by the Database Administrators from test databases to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT