Question

In: Computer Science

I have a countdown sequence i have to do for a code, when the result is...

I have a countdown sequence i have to do for a code, when the result is entered it starts to countdown, i tried imputting some codes but i am clueless ( javascript):

/*******************************************************************************

* Problem 3: create count-down number sequence strings

*

* A count-down sequence is a String made up of a descending list of numbers.

* For example, the count-down sequence for the number 3 would be:

*

* "321"

*

* Write the countDownSequence function below, allowing any number between

* 1 and 10 to be accepted.  Otherwise, throw an error (see

* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)

*

* If we called countDownSequence(5) we'd expect to get:

*

* "54321"

*

* If we called countDownSequence(55) we'd expect to have an error thrown

*

* Error: only start values between 1 and 10 are valid

*

* @param {Number} start - the number to start counting down from (must be 10 or smaller).

******************************************************************************/

function countDownSequence(start) {

  var ret = '';

  while (start >= 0) {

    ret += start;

    if (start > 0) {

      ret += '';

    }

    start--;

  }

  return ret;

  // Your code here...

}

It has to pass these tests:

const { countDownSequence } = require('./solutions');

describe('Problem 3 - countDownSequence() function', function() {

  test('proper count down sequence from 10', function() {

    let start = 10;

    let result = countDownSequence(start);

    expect(result).toBe('10987654321');

  });

  test('proper count down sequence from 1', function() {

    let start = 1;

    let result = countDownSequence(start);

    expect(result).toBe('1');

  });

  test('starting at 0 should throw', function() {

    function shouldThrow() {

      // Using 0 is invalid, this should throw

      countDownSequence(0);

    }

    expect(shouldThrow).toThrow();

  });

  test('starting at a negative number should throw', function() {

    function shouldThrow() {

      // Using -100 is invalid, this should throw

      countDownSequence(-100);

    }

    expect(shouldThrow).toThrow();

  });

  test('starting at a number greater than 10 should throw', function() {

    function shouldThrow() {

      // Using 11 is invalid, this should throw

      countDownSequence(11);

    }

    expect(shouldThrow).toThrow();

  });

});

Solutions

Expert Solution

Try replacing the code of the function "countDownSequence()" with the below code and the code is as follows:

function countDownSequence(start) {

                        var ret = '';
                        try {
                                if (start >= 0 && start <= 10) {
                                        while(start >=0 ){
                                                ret += start;
                                                start--;
                                        }
                                        return ret;
                                }
                                else {
                                throw "Error: only start values between 1 and 10 are valid";
                                }
                        }catch(err){
                                console.log(err)
                        }
                        
                  
                }

This implementation of the countDownSequence should pass the test cases stated in the question.

I am attaching the output sample, I am just running the html file which contain the above code and calling the countDownSequence function with different parameter

If you face any problem let me know in the comment section. I will try to solve it.


Related Solutions

hi! I have this code. when I run it, it works but in the print(the number...
hi! I have this code. when I run it, it works but in the print(the number we are searching for is ) it prints the number twice. ex. for 5 it prints 55 etc. apart from this, it works #include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ int n,i; printf("Give me the size of the table : \n"); scanf("%d", &n); int A[n],x,y; for (i=0;i<n;i++) A[i]=rand() % n; for (i=0;i<n;i++) printf("%d\n", A[i]); srand(time(NULL)); y=rand()% n ; printf("The number we are searching...
Hello I have this error in the code, I do not know how to fix it....
Hello I have this error in the code, I do not know how to fix it. It is written in C++ using a Eclipse IDE Error: libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string bus.h =========== #pragma once #include using namespace std; class Bus { private:    string BusId; // bus ID    string Manufacturer; // manufacturer of the bus    int BusCapacity; // bus capacity    int Mileage; // mileage of bus    char Status; // current status...
I know how to do this with arrays, but I have trouble moving my code to...
I know how to do this with arrays, but I have trouble moving my code to use with linked lists Write a C program that will deal with reservations for a single night in a hotel with 3 rooms, numbered 1 to 3. It must use an infinite loop to read commands from the keyboard and quit the program (return) when a quit command is entered. Use a switch statement to choose the code to execute for a valid command....
I have to complete all //to do comments for the following code: /** * A ShoppingBasket...
I have to complete all //to do comments for the following code: /** * A ShoppingBasket holds zero or more Products and can provide information * about the Products. One can add Products to a ShoppingBasket during its * lifetime, reset the ShoppingBasket, create a copy which contains Products of * at least a certain value, and make various queries to the ShoppingBasket. * (Thus, the number of Products that will be stored by a ShoppingBasket object * is not...
I have the following python code. I get the following message when running it using a...
I have the following python code. I get the following message when running it using a test script which I cannot send here: while textstring[iterator].isspace(): # loop until we get other than space character IndexError: string index out of range. Please find out why and correct the code. def createWords(textstrings): createdWords = [] # empty list for textstring in textstrings: # iterate through each string in trxtstrings iterator = 0 begin = iterator # new begin variable while (iterator <...
hello! So I have this CIS assignment lab but when I try to make the code...
hello! So I have this CIS assignment lab but when I try to make the code I don't really know where to start from. My professor is very hard and he likes to see the outcomes as they are shown in the problem. Please help me! Write a program that can be used as a math helper for an elementary student. The program should display two random integer numbers that are to be added, such as:     247 + 129...
hello! So I have this CIS assignment lab but when I try to make the code...
hello! So I have this CIS assignment lab but when I try to make the code I don't really know where to start from. My professor is very hard and he likes to see the outcomes as they are shown in the problem. Please help me! the program should be a C++ PLS Write a program that can be used as a math helper for an elementary student. The program should display two random integer numbers that are to be...
Write a java program that prints to the screen a countdown 2,4,6,8, and then "Who do...
Write a java program that prints to the screen a countdown 2,4,6,8, and then "Who do we appreciate!" (hint use a for loop). Create a java program which prints out a random goodwill message i.e. (Have a great day!) please have at least 4 messages. Write a java program that will ask the user to enter a number and print out to the screen until the number -3 is entered. Write a JAVA program that calls a method that finds...
How do I add the information below to my current code that I have also posted...
How do I add the information below to my current code that I have also posted below. <!DOCTYPE html> <html> <!-- The author of this code is: Ikeem Mays --> <body> <header> <h1> My Grocery Site </h1> </header> <article> This is web content about a grocery store that might be in any town. The store stocks fresh produce, as well as essential grocery items. Below are category lists of products you can find in the grocery store. </article> <div class...
Do not use arrays and code a program that reads a sequence of positive integers from...
Do not use arrays and code a program that reads a sequence of positive integers from the keyboard and finds the largest and the smallest of them along with their number of occurrences. The user enters zero to terminate the input. If the user enters a negative number, the program displays an error and continues. Sample 1: Enter numbers: 3 2 6 2 2 6 6 6 5 6 0 Largest Occurrences Smallest Occurrences 6 5 2 3. Do not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT