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...
. 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 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...
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...
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...
Using the R package to answer the following two questions. You MUST submit your R code...
Using the R package to answer the following two questions. You MUST submit your R code for analysis. 2. Below are heights for a simple random sample of n = 15 young trees (in cm). (50 pts) 27, 33, 33, 34, 36, 37, 39, 40, 40, 41, 41, 42, 44, 46, 47. (a) Test the hypothesis that the mean tree height is equal to 38 cm. (b) Calculate the 95% confidence interval for the population mean of young trees. (c)...
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using namespace std; int lastIndexOf(char *s, char target) { int n=strlen(s); for(int i=n-1;i>=0;i--) { if(s[i]==target) { return i; } } return -1; } void reverse(char *s) { int n=strlen(s); int i=0,j=n-1; while(i<=j) { char temp=s[i]; s[i]=s[j]; s[j]=temp; i++; j--; } return; } int replace(char *s, char target, char replacementChar) { int len=strlen(s); int total=0; for(int i=0;i<len;i++) { if(s[i]==target) { s[i]=replacementChar; total+=1; } } return total;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT