Question

In: Computer Science

Using JavaScript You must submit a file, called indentation.js indentation.js: program to indent The following program...

Using JavaScript

You must submit a file, called indentation.js

indentation.js: program to indent


The following program is difficult to follow because of its indentation:


print ( "binary");
var n = Math.pow (2, 31) - 1;
var bits = 0;
while (n> 0) {
bits + = n & 1;
n >> = 1;
}
print ("Number of bits at 1:" + bits);
for (var i = 1; i <= bits; i ++) {
var str = "";
if (i% 3 == 0) {str + = "Fizz";}
if (i% 5 == 0) {str + = "Buzz";}
if (i% 3! = 0) if (i% 5! = 0)
str + = i;
print (str);
}
var x = 2;
var y = 4;
var z = 4;
for (var i = 0; i <x; i ++) {
for (var j = 0; j <y; j ++) {str = ""; for (var k = 0; k <z; k ++) {
str + = j == k? 'x': '';
}
print (str);} if (i <x - 1) print ("");


You must make the program above more readable by indenting each line correctly.

Remember that:

* The content of a conditional structure (for, if, while, ...) should be indented by one more level (4 spaces or a tab)

* There should be only one statement per line (and we do not put a "}" on the same line as an instruction)

* Nested structures require to indent more deeply the code:

// Code excluding conditional structures
print ( "test");

if (condition) {
    // Everything in this if should be indented by 4 spaces
    print ( "Hello");
    while (condition) {
        // Everything in this while is already indented
        // of 4 spaces and is indented by 4 other spaces = 8 spaces
        print ( "...");
    }
}

Solutions

Expert Solution

Please find the indented code (follow the screenshot) below.

CODE

print ( "binary");
var n = Math.pow (2, 31) - 1;
var bits = 0;
while (n > 0) {
bits += n & 1;
n >>= 1;
}
print ("Number of bits at 1:" + bits);
for (var i = 1; i <= bits; i ++) {
var str = "";
if (i % 3 == 0) {
str += "Fizz";
}
if (i% 5 == 0) {
str += "Buzz";
}
if (i % 3 != 0) {
if (i % 5 != 0) {
str += i;
}
print (str);
}
}
var x = 2;
var y = 4;
var z = 4;
for (var i = 0; i < x; i ++) {
for (var j = 0; j < y; j ++) {
str = "";
for (var k = 0; k < z; k ++) {
str += j == k ? 'x' : '';
}
print (str);
}
if (i < x - 1) {
print ("");
}
}


Related Solutions

Using JavaScript You must submit a file called, called pass.js pass.js: Validator of passwords Your task...
Using JavaScript You must submit a file called, called pass.js pass.js: Validator of passwords Your task this week will be to program a valid password validator. A password is safe only if it satisfies the following constraints: - It has from 5 to 10 characters (inclusive) - It contains at least one lowercase letter, at least one capital letter and at least one number - The first character is different from the last Once the password is entered and validated,...
JavaScript 1. FizzBuzz Submit js file with functioning code Write a program that uses console.log to...
JavaScript 1. FizzBuzz Submit js file with functioning code Write a program that uses console.log to print all the numbers from 1 to 120, with two exceptions. For numbers divisible by 4, print "Fizz" instead of the number, and for numbers divisible by 10 (and not 4), print "Buzz" instead. When you have that working, modify your program to print "FizzBuzz", for numbers that are divisible by both 4 and 10 (and still print "Fizz" or "Buzz" for numbers divisible...
using C thank you Must submit as MS Word file with a screenshot of the 3...
using C thank you Must submit as MS Word file with a screenshot of the 3 outputs. Run your program 3 times. the output must be in a screenshot not typed for the each time you run the program. thank you Modify the code below to implement the program that will sum up 1000 numbers using 5 threads. 1st thread will sum up numbers from 1-200 2nd thread will sum up numbers from 201 - 400 ... 5th thread will...
Using C++ In a separate header file: Create a new type called "Patient" - you must...
Using C++ In a separate header file: Create a new type called "Patient" - you must use a  class. Give your "Patient" type at least five (5) member elements of your choosing, and at least one member function. You should have member elements to hold patient name, and visitReason (which can change), and other items of your choosing. In your cpp file: Create at least one instance of Patient type (example: CurrentPatient ). Create a menu-driven program OF YOUR OWN DESIGN...
. You must use Excel (submit either a pdf, word or Excel file only). . You...
. You must use Excel (submit either a pdf, word or Excel file only). . You must identify the 5 steps (you must address each in detail). Problem: Use the given data to complete a t-test using Excel. Question: Is there a difference in group means between the number of words spelled correctly for two groups of fourth graders? Group Assignment Score 1 3 1 4 1 10 2 14 2 7 2 8 2 10 2 15 2 9...
Write the programs in JavaScript: Write a program that reads a text file and outputs the...
Write the programs in JavaScript: Write a program that reads a text file and outputs the text file with line numbers at the beginning of each line.
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which...
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which has the following data (you’ll have to create pop.dat) AX013 1.0 BX123456 1234.56 ABNB9876152345 99999.99 The data are account numbers and balances. Account numbers are, at most 14 characters. Balances are, at most, 8 characters (no more than 99999.99). Using a loop until the end of file, read an account number and balance then write these to standard output in the following format shown...
This document describes a computer program you must write in Python and submit to Gradescope. For...
This document describes a computer program you must write in Python and submit to Gradescope. For this project, the program will simulate the operation of a vending machine that sells snacks, accepting coins and dispensing products and change. In an actual vending machine, a program running on an embedded computer accepts input from buttons (e.g. a numeric keypad) and devices such as coin acceptors, and its output consists of signals that control a display, actuate motors to dispense products, etc.....
Write a single MATLAB function called numdiff, saved in a file called numdiff.m. Submit that single...
Write a single MATLAB function called numdiff, saved in a file called numdiff.m. Submit that single file here in Canvas. To make grading easier, please do the following: • Close your function with "end". • Don't have your function print anything to the console; just return the output. To accomplish this: o Put a semicolon ";" at the end of each statement. o Put a percent "%" before each disp or fprintf statement. The function numdiff should take the following...
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT