Skip to main content

JSchallenger

Fundamentals

比较字符

Question

Write a function that takes two values, say a and b, as arguments.Return true if the two values are equal and of the same type.

// my code
function myfunction(a,b){
let flag;
if (a===b){
flag=true;
}
else{
flag=false;
}
return flag;
}
// Answer
function myFunction(a, b) {
return a === b;
}
Question

Write a function that takes a string as argument(参数).Extract the last 3 characters (返回最后三个字符) from the string.Return the result.

// my code
function myFunction(str) {
let str1=str.substr(str.length-3,3);
return str1;
}
// answer
function myFunction(str) {
return str.slice(-3);
}

删除字符串的前几个字符

Question

Write a function that takes a string (a) as argument.Remove the first 3 characters of a.Return the result.

Using the method slice:

function myFunction(a) {
return a.slice(3);
}

Or using the method slicesubstring:

function myFunction(a) {
return a.substring(3);
}