forked from LaunchCodeEducation/javascript-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-modification.js
More file actions
22 lines (21 loc) · 1.27 KB
/
Copy pathstring-modification.js
File metadata and controls
22 lines (21 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const input = require('readline-sync');
let str = "LaunchCode";
let num1 = Number(input.question(`Enter the number of letters to be relocated greater than or equal to 0 and less than ${str.length}: `));
let error = 'There are no errors found! '
//1) Use string methods to remove the first three characters from the string and add them to the end.
//Hint - define another variable to hold the new string or reassign the new string to str.
if (num1 > str.length) {
num1 = 3;
error = `You have entered a number larger than ${str.length}! Using default value of 3. `;
} else if (num1 < 0) {
num1 = 3;
error = `You have entered a negative number! Using default value of 3. `;
}
let arr = str.split("");
let pigLatin = arr.splice(0, num1).join("");
arr.splice(arr.length, 0, pigLatin);
let str2 = arr.join("");
//Use a template literal to print the original and modified string in a descriptive phrase.
console.log(`${error}The original string of ${str} in pseudo-pig latin is: ${str2}.`);
//2) Modify your code to accept user input. Query the user to enter the number of letters that will be relocated.
//3) Add validation to your code to deal with user inputs that are longer than the word. In such cases, default to moving 3 characters. Also, the template literal should note the error.