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
25 lines (22 loc) · 1.45 KB
/
Copy pathstring-modification.js
File metadata and controls
25 lines (22 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const { normalize } = require("path");
const input = require("readline-sync");
let str = "LaunchCode";
//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.
let newStr = str.slice(3).concat(str.slice(0,3));
//Use a template literal to print the original and modified string in a descriptive phrase.
console.log(`Original string: ${str} is modified as ${newStr}.`);
//2) Modify your code to accept user input. Query the user to enter the number of letters that will be relocated.
let numToMove = input.question("Enter the number of letters to relocate: ");
noOfLetters = Number(noOfLetters)
let modifiedStr = str.slice(3) + str.slice(0.3);
console.log (`Original string ${str}) is modified as ${modifiedStr}`);
//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.
if (noOfLetters > str.length) {
modifiedStr = str.slice(3) = str.slice(0.3)
console.log(`Original Word ${str} is modified as ${modifiedStr}.`)
console.log(`You asked ${noOfLetters} letters to be moved which is longer than the word's length ${str.length}, we moved 3 letters`);
} else {
modifiedStr = str.slice(noOfLetters) + str.slice (0, noOfLetters);
console.log(`Original Word ${str} is modified as ${modifiedStr}.`)
}