Daily Coding Day One Aught Eight

My Paps used to say aught versus zero so I thought this would be a nice shoutout to the suspenders wearing gent. Another few days another programming problem. This one was marked as easy, but unless you have a good handle on String maniupluation it would be hard.

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
26
27
28
//This problem was asked by Google.
//Given two strings A and B, return whether or not A can be
//shifted some number of times to get B.
//For example, if A is abcde and B is cdeab, return true.
//If A is abc and B is acb, return false.


function changeContent () {
var myelement = document.getElementById("myelement");
myelement.innerHTML= canShift('abcde','cdeab')
+ ' or '
+ canShift('abc','acb');

}

function canShift(strA, strB)
{
if (strA.length < strB)
{
return false;
}else
{
let concatA = strA + strA;
return concatA.includes(strB);
}
}

window.onload = changeContent ;

I was too lazy to remove the default JSFiddle stuff, but the crux is the concatenation step. By concatenating the first string with itself you get all of the loops and would be able to discern whether the second string is a shifted version of the first. Fun, but one of the worst kinds of interview problems because it is obtuse and there is zero practicality. It can be obtuse OR not practical, but come on…not both.