0

thanks for reading. I am currently writing an assignment where I am tasked with making functions while having a list of 'banned' methods for the assignment. The function in question takes a string from the user, then takes a character in that string and moves it either right or left based on a final value taken from the user.

i.e. String = Word, Character = W, Value = +2(right). Final product = orWd

Obviously the banned methods are the ones which would be most useful as the assignment is meant to teach you how to achieve the purpose of these methods using loops, arrays and functions.

I have gotten up to finding and selecting the character to move, but am stuck figuring out how to move this character through the string. I've considered rebuilding the entire string as a new string but this seems inefficient.

var x=prompt("Please enter a word.");
var y=prompt("Please enter a character to select.");
var z=prompt("Please enter a value to move the selected character by " + " (Negative values move left, positive values move right.)");
function questionOne(stringInput,charInput,numInput) {
 var stringLength= stringInput.length;
 for(i=0;i<stringLength;i++){
 var stringChar=stringInput.charAt(i);
 if (stringChar === charInput){
 var newWord;
 for (count=0;count<stringLength;count++){
 
 }
 }
 }

EDIT: The complete list of 'banned' methods.

String built-in functions

endsWith()

includes()

indexOf()

lastIndexOf()

localeCompare()

match()

repeat()

replace()

search()

slice()

split()

startsWith()

substr()

substring()

toLocaleLowerCase()

toLocaleUpperCase()

toLowerCase()

toString()

toUpperCase()

trim()

trimLeft()

trimRight()

valueOf()

Array built-in functions

concat()

copyWithin()

every()

fill()

filter()

find()

findIndex()

forEach()

indexOf()

isArray()

join()

lastIndexOf()

map()

pop()

push()

reduce()

reduceRight()

reverse()

shift()

slice()

some()

sort()

splice()

toString()

unshift()

valueOf()

I've searched high and low for a solution but haven't been able to find one. Thanks for any help.

3
  • 1
    which methods are allowed? which not? Commented Jul 26, 2018 at 21:17
  • we need to know which methods are allowed, i would assume some of the string methods are banned but can't figure out exacly which ones are Commented Jul 26, 2018 at 21:24
  • Just posted in edit, the original list is an image I couldn't extract from the document so I had to recreate the list. Sorry about that. Commented Jul 26, 2018 at 21:29

2 Answers 2

1

Just an idea, without giving an answer for a part problem, to move a character with a positive value to the right side.

For example take a longer word and choose 'wording' and the character 'r' and 2 for moving the letter two places to the right side.

 0 1 2 3 4 5 6 indices
 v position of character before reordering
 w o |r d i| n g original word
 w o |d i r| n g result
 ^ position of character after reordering
 0 0 1 1 0 0 0 offset

What you can see is

  • if the character 'r' is not found, take the letter at the index,

  • if the character 'r' is found, use an offset of one and the actual index for getting a letter,

  • if the index is equal to the postion of swapping character plus the move value, then take the character 'r' of the saved position and set the offset for the index to zero,

  • proceed until end of string.

answered Jul 26, 2018 at 22:00
Sign up to request clarification or add additional context in comments.

Comments

0

read input letter by letter

the main purpose of that is to fill an array of letters, therefor it would be easy to treat it after.

to do that you need to read input from an input field and not a prompt so you can attach an event listener to the input field to read and store the clicked letter.

here's is a code snippet to achieve that:

let word = []; // word array
let input = document.querySelector('#txt');
input.addEventListener( "keyup", function(e){
 word[word.length] = e.key;
});
<input id="txt" value="" type="text">

Function to compose the word

You need to create a function that reads params and compose the result word and since array methods are banned from use, why not create a custom function to help us achieve our goal.

here's a snippet of code to achieve that ( first snippet included ) :

let wArr = []; // word array
let input = document.querySelector('#txt');
let submit = document.querySelector('#submit');
// read input from user letter by letter and save it to wArr
input.addEventListener( "keyup", function(e){
 wArr[wArr.length] = e.key;
});
// submit
submit.addEventListener( "click", composeWord);
function composeWord(){
 
 // read params
 var c = document.querySelector("#letter").value;
 var d = document.querySelector("#direction").value;
 var n = parseInt(document.querySelector("#number").value);
 
 // loop through each char
 for( var i=0; i < wArr.length; i++ ){
 
 // target char encountered ?
 if( wArr[i] === c ){
 
 // select the char to switch with
 // we use conditional statements here
 var e = d === "right" ? i + n + 1 : i - n ;
 
 // perform a permutation
 // use a simple permutation logic
 wArr = shiftArr( wArr, i, e, wArr[i] );
 
 break;
 }
 }
 // compose the word
 let w = "";
 for( let i=0; i< wArr.length; i++){
 w+= wArr[i];
 }
 
 alert(w);
}
// custom function to shift an array
function shiftArr( arr, i1, i2, val ){
 
 let a = [];
 
 let j = 0;
 
 for( var i=0; i< arr.length; i++ ){
 
 
 if( i === i1 )
 continue;
 
 else if( i === i2 ){
 a[j] = arr[i1];
 j++;
 }
 
 a[j] = arr[i];
 j++;
 }
 
 return a;
}
<input id="txt" value="" type="text" placeholder="Type the desired word">
<input type="text" value="" id="letter" placeholder="Enter the letter to shift">
<input type="text" value="" id="direction" placeholder=" 'right' or 'left'" >
<input type="text" value="" id="number" placeholder='Number of positions to shift'>
<button id='submit'>Submit</button>

END

This is a working naive code, and what i mean by naive is that the user must always enters the right data due to the fact that the code doesn't check for errors neither catch them.

answered Jul 27, 2018 at 5:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.