In: Computer Science
Create a JavaScript program that asks for the input of at least five words (a total of 25+ characters) string from a user and performs the following functions to it:
STRING METHODS
Example: Input 5 words
Note: This is an example with example input. Your input will make this much different so don't hard code my examples into your code. The program should ask for input for the 5 words.
Enters: I think therefore I am
Check to make sure it is 25 characters in length. (step 1) - it is not, it is only 22 characters so have them try again.
Enters: Failure is success in progress (this is over 25 characters)
Output: Failure is success in progress (step 2)
Output in lowercase: failure is success in progress (step 3)
Output in uppercase: FAILURE IS SUCCESS IN PROGRESS (step 4)
Output character count: Your string has 30 characters. (step 5)
Output characters between index 5 and 10. re is (step 6)
Output 5 characters starting from position 15. ess i (step 7)
Combine with a 25+ character string you have created: Failure is success in progress Success is dependent on effort (step 8)
ARRAY METHODS - Working with the same string as the String Methods program, continue your program by completing these steps.
Example continued: Array Methods
Your list should be output of all words in your combined string: (step 1)
failure
is
success
in
progress
Success
is
dependent
on
effort
Output: Your string contains 10 words. (step 2)
Output after removing the first array item (step 3)
is
success
in
progress
Success
is
dependent
on
effort
Output after adding the word banana to the end of the array (step 4)
is
success
progress
Success
is
dependent
on
effort
banana
Output after reversing the array elements (step 5)
banana
effort
on
dependent
is
Success
progress
success
is
Output after updating the second array item with the word stardust (step 6)
banana
effort
stardust
dependent
is
Success
progress
success
is
Output after sorting the array (step 7)
Success
banana
dependent
effort
is
is
progress
stardust
success
<input id="enter"></input>
<button id="check" onclick="myFunc(this);">Enter</button>
<div id="showcheck"></div>
<script type="text/javascript">
let text = document.getElementById("enter");
let btn = document.getElementById("check");
let output = document.getElementById("showcheck");
//declaring a function to wrap steps to be performed
let myFunc = function (e){
//reset the outout div
output.innerText = "";
//get the text
let str = text.value;
//check if length matches requirement
if(text.value.length <= 25){
//show retry message
alert("String length is less than 25 characters. PLease try again!");
//reset the textbox
text.value = "";
}
else{
//get the value
str = text.value;
//show it
output.innerText += "\nOutput: "+str+"\n";
//lowercase
output.innerText += "\nOutput in lowercase: " + str.toLowerCase()+"\n";
//uppercase
output.innerText += "\nOutput in uppercase: " + str.toUpperCase()+"\n";
//character count
output.innerText += `\nOutput character count: Your string has ${str.length} characters.\n`;
//character bwetween 5 and 10
output.innerText += `\ncharacters between index 5 and 10. ${str.slice(5,10)}\n`;
// 5 charcaters after index 15
output.innerText += `\nOutput 5 characters starting from position 15. ${str.slice(15, 20)}\n`;
//create your own string
let created_str = "Success is dependent on effort.";
//concat the strings
str = str +" "+ created_str;
output.innerText += `\nCombine with a 25+ character string you have created: ${str}\n`;
//convert words to a array
let arr = str.split(" ");
output.innerText += `\nArray: \n`;
for (let i of arr){
output.innerText += `${i}\n`;
}
//output total number of elements
output.innerText += `\nYour string contains ${arr.length} words.\n`;
//remove 1st element
arr.shift();
//display
output.innerText += `\nAfter removing 1st item,\n`;
for (let i of arr) {
output.innerText += `${i}\n`;
}
//add banana
arr.push("banana");
//display
output.innerText += `\nAfter adding banana to the last,\n`;
for (let i of arr) {
output.innerText += `${i}\n`;
}
//reverse the array
arr = arr.reverse();
//display
output.innerText += `\nAfter reversing array,\n`;
for (let i of arr) {
output.innerText += `${i}\n`;
}
// after updating the second array item with the word stardust
arr[2] = "stardust";
//display
output.innerText += `\nafter updating the second array item with the word stardust,\n`;
for (let i of arr) {
output.innerText += `${i}\n`;
}
console.log(arr);
//after sorting the array
arr.sort();
//display
output.innerText += `\nafter sorting,\n`;
for (let i of arr) {
output.innerText += `${i}\n`;
}
//clear the text field
text.value = "";
}
}
</script>