Confirm the ending - need help with arrays

How I can add target array to str array ?

Link to quiz : https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/confirm-the-ending/

function confirmEnding(str, target) {
  // "Never give up and good luck will find you."
  // -- Falcor
let myStr = str.split('');
console.log(myStr);
let targetArr = target.split('');
console.log(targetArr);


let newTargetArray = myStr.splice(myStr.length-targetArr.length,targetArr.length,targetArr);
console.log(newTargetArray);

  return str;
}

confirmEnding("Congratulation", "on");

Hi, I’ve changed your thread title to describe your problem more clearly. Descriptive titles, such as “Why does my function return undefined?”, are more likely to elicit helpful responses than generic titles, such as “Help please”.

you have already created a few posts with the most generic titles - you may want to try to be more descriptive

I don’t know why you want to join two arrays, but you can do so using concat()

Thanks ) concat just add elements from one array to anouther , I need to shift the last elements of first array to elements of anouther elements )

Do you mean that you want to add the elements of the first array in front of the elements of the second array? :thinking:
Your text is very hard to understand, it’s full of typos, please try to pay attention. May I suggest you give a try to LanguageTool for chrome and Firefox it’s free and open source, it helps you write better.:hugs:

Hello ) I want to create new array by using splice from two arrays above

remember that splice changes the array on which it is used on and return the removed elements, so you may also want to check what’s the value of myStr

you have different arrays than before with splice

Thank you ) I understand ) but how add condition to check if the arrays is same ?

function confirmEnding(str, target) {
  // "Never give up and good luck will find you."
  // -- Falcor
const constStr= str.split('');
let myStr = str.split('');
console.log(myStr);
let targetArr = target.split('');
console.log(targetArr);


let newTargetArray = myStr.splice(myStr.length-targetArr.length,targetArr.length,targetArr);
console.log(myStr);
if (myStr == constStr) {
  console.log("yes");
} else {  console.log("not"); }

  return str;
}

confirmEnding("Congratulation", "on");

be careful with the splice, after it myStr has value of ["C", "o", "n", "g", "r", "a", "t", "u", "l", "a", "t", "i", ["o", "n"]]

I suggest using this tool as debugging tool: http://pythontutor.com/javascript.html

once you have a bit more practice you could just use the browser console, or an editor like repl.it so that the console has less limitations

you also can’t do this with arrays. you could use a loop to check that each element equals the other, or make them strings before comparing

function confirmEnding(str, target) {
  // "Never give up and good luck will find you."
  // -- Falcor
const constStr= str.split('');
let myStr = str.split('');
console.log(myStr);
let targetArr = target.split('');
console.log(targetArr);


let newTargetArray = myStr.splice(myStr.length-targetArr.length,targetArr.length,targetArr);
console.log(myStr);
let j = 0;
let bll= true;
for ( let i = 0; i < constStr.length; i++ ){

  if (constStr[i] !== myStr[j]){

    return false
 
  } else { 
    bll =  true;

    }
    j++
}  console.log(bll);
return bll ;
 } 

confirmEnding("Congratulation", "on");

Problems with returning true false

do you really need two different variables for the loop? can’t you use only one?


still the same issue with splice, which also make the check fail on principle (as you end with "o" !== ["o", "n"])


is the value of bll changed? you don’t need this. do you even need bll? you have return false inside the loop, you could also have return true instead of return bll

in this line of function

changed the question

function confirmEnding(str, target) {
  // "Never give up and good luck will find you."
  // -- Falcor
const constStr= str.split('');
let myStr = str.split('');
console.log(myStr);
let targetArr = target.split('');
console.log(targetArr);


let newTargetArray = myStr.splice(myStr.length-targetArr.length,targetArr.length,targetArr);
console.log(myStr);

let bll= true;
for ( let i = 0; i < constStr.length; i++ ){

  if (constStr[i] !== myStr[i]){
   return false;
 
  } else { 


    }


}  

 } 

confirmEnding("Congratulation", "on");

Thanks ) but now how I can return true ?

in the same way you were before, you just don’t need the variable
to do it

you also really need to fix the issue with the polidimensional array… or you will never pass the challenge

Can you give some link to this ?

what exactly is that you need a link for? what is a polidimensional array?
a polidimensional array is an array that contains array. maybe multidimensional is more understable and means the same thing

here together my paragraphs on it: