Question

Problem: Obscure Message You decide to write a program that obscures a sentence for you. You...

Problem: Obscure Message

You decide to write a program that obscures a sentence for you. You will write a function Obscure() that takes three string arguments and returns one string argument. The first argument is a substitution key, the second a substitution value, and the third a sentence. The program first uses substitution to obscure the sentence and then prepends AND appends the substitution value followed by and preceded by a space " ", respectively. The program should then return the obscured sentence to the caller. You can use the following shell to start with the given parameters below:

//assignment1.js

function Obscure(sub_key, sub_value, sentence) {
    // your code here
    ...
    return obscuredSentence;
}

Example Test Case(s):

Arguments: "the", "the goat", "I want the money"
Returns: "the goat I want the goat goat money the goat"

Arguments: "the", "chicken", "I want the money"
Returns: "chicken I want chicken money chicken"

Obscure() should satisfy the following:

  • The substitution key and value can be strings that contains only letters.
  • If the sentence contains the key anywhere, it is replaced in the sentence with the substitution value.
  • The substitution is recursively performed up to 2 times. If it cannot be completed up to 2 times, it will simply move on to the next part of the program. For this, you may use global variables if needed.
  • Use more than one function to complete the program.
  • Use JavaScript built-in functions.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Javascript Code:
var recurse_count = 0;
function Obscure(sub_key, sub_value, sentence) {
   if(recurse_count==2)return sentence; // two Perform recursion only two times
var sentence1 = sentence.replace(sub_key , sub_value);
recurse_count++;
return Obscure(sub_key , sub_value, sentence1);
}

function prepends(sub_value, sentence){
   return sub_value + " " + sentence;
}


function appends(sub_value, sentence){
   sentence = sentence.concat(" ");
   sentence = sentence.concat(sub_value);
   return sentence;
}

sub_key = "the"
sub_value = "the goat"
sentence = "I want the money"

sentence = Obscure(sub_key, sub_value, sentence);
sentence = prepends(sub_value, sentence);
sentence = appends(sub_value, sentence);
console.log(sentence);

Output:

var recurse count = 0; function Obscure (sub_key, sub_value, sentence) f if (recurse_count-2)return sentence; var sentencel -

var recurse count -6; function Obscure (sub_key, sub_value, sentence) f if (recurse_count-2) return sentence; var sentencel -

Hit Like for Support! :)

Add a comment
Know the answer?
Add Answer to:
Problem: Obscure Message You decide to write a program that obscures a sentence for you. You...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. In this lab, you will create a simple encryption function that will require a sentence...

    1. In this lab, you will create a simple encryption function that will require a sentence and a key (both strings) as a parameter and return an encrypted version of the string. The encryption algorithm will use the exclusive OR operator (commonly abbreviated as XOR). The general structure of the encryption is that every position in the sentence is XOR'd with the accompanying position of the key. If the sentence is longer than the key, you repeat the key. For...

  • in c++ Program 1 Write a program that sorts a vector of names alphabetically using the...

    in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...

  • Write a program that reads a sentence input from the user. The program should count the...

    Write a program that reads a sentence input from the user. The program should count the number of vowels(a,e,i,o,u) in a sentence. Use the following function to read the sentence. Should use switch statement with toupper. int read_line(char str[],int n) {    int ch, i=0;    while((ch =getchar()) != '\n')        if(1<n)            str[i++]==ch;    str[i] != '\0';    return i; } output should look like: Enter a sentence: and thats the way it is! Your sentence...

  • 1) Echo the input: First, you should make sure you can write a program and have...

    1) Echo the input: First, you should make sure you can write a program and have it compile and run, take input and give output. So to start you should just echo the input. This means you should prompt the user for the plaintext, read it in and then print it back out, with a message such as "this is the plaintext you entered:". [4 points, for writing a working program, echoing the input and submitting the program on the...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • Study the VIGENÈRE CIPHER and implemented it with c++ 1.In your program, you should have two...

    Study the VIGENÈRE CIPHER and implemented it with c++ 1.In your program, you should have two functions: encryption and decryption. 2. Use any key you like. (Don't use deceptive in the slides) 3. choose a sentence or a paragraph you like as the plaintext. I have the code I just need the implementation in a different way // C++ code to implement Vigenere Cipher #include<bits/stdc++.h> using namespace std; // This function generates the key in // a cyclic manner until...

  • Task 1: String with Loop • Write a program that reads a sentence and use a...

    Task 1: String with Loop • Write a program that reads a sentence and use a loop that counts how many elements in a string that are equal to ‘t’. Task 2: List with Function • Write a function fill that fills all elements of a list with a given value. • For example: o The call fill(scores, 13) should fill all elements of the list scores with the value 13. • Write a demo program to show how this...

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • In C++ syntax please Write a program that implements and demonstrates a linked list using functions....

    In C++ syntax please Write a program that implements and demonstrates a linked list using functions. Your program should first dehne a node that stores an integer and then your program will include the following functions appendo- This function accepts the head pointer (by reference) of a linked list and an integer as it's only arguments. It then creates a node, stores the integer argument in the node, and adds it to the end of the list. findo-This function accepts...

  • Use c-strings for the following project: Write a C++ program that declares an array containing up...

    Use c-strings for the following project: Write a C++ program that declares an array containing up to a maximum of 20 sentences, each sentence of maximum 81 characters long, using c-strings. Continue reading sentences from the user and store them in the array of sentences, until the user enters a NULL string for the sentence or 20 sentences have been entered. Then, one by one, display each sentence entered by the user and present the following menu of operations on...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT