function findLongestWord(str) {
var strArray = str.split(" ");
var strLength=0;
for(i=0; i<strArray.length; i++){
if(strArray[i].length>strLength){
strLength = strArray[i].length;
console.log("i="+i);
console.log("strLength="+strLength);
}
}
return strLength;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
我的第二種,用max
function findLongestWord(str) {
// 请把你的代码写在这里
strArray = str.split(" ");
numArray = [];
for(i=0; i<strArray.length; i++){
numArray[i]=strArray[i].length;
}
biggestLength = Math.max(...numArray);
return biggestLength;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
function confirmEnding(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
var strEnd = str.substring(str.length - target.length, str.length);
return strEnd == target;
}
confirmEnding("Bastian", "n");
官方解答
function confirmEnding(str, target) {
return str.substr(-target.length) === target;
}
function repeatStringNumTimes(str, num) {
// repeat after me
var timeStr=[];
for(i=0; i<num; i++){
timesStr=timeStr.push(str);
}
return timeStr.join("");
}
repeatStringNumTimes("abc", 3);
function truncateString(str, num) {
// Clear out that junk in your trunk
var dotstr;
if(num>=str.length){
dotstr = str;
}
else if(num<3){
dotstr = str.slice(0,num)+"...";
}
else if(num<str.length){
dotstr = str.slice(0,num-3)+"...";
}
console.log(dotstr);
return dotstr;
}
truncateString("A-tisket a-tasket A green and yellow basket", 11);
官方的進階解答
function truncateString(str, num) {
if (str.length <= num) {
return str;
} else {
return str.slice(0, num > 3 ? num - 3 : num) + '...';
}
function bouncer(arr) {
// Don't show a false ID to this bouncer.
return arr.filter( (val) => Boolean(val) != false );
}
bouncer([7, "ate", "", false, 9]);
留言串看到的解法
function bouncer(arr) {
var a = [];
arr.forEach(function(el){
if(el){
a.push(el);}});
return a;}
列舉式的寫法
function badValues(val){
return val !== false && val !== null && val !== 0 && val !== "" && val !== undefined && !Number.isNaN(val);
}
function bouncer(arr) {
return arr.filter(badValues);
}
bouncer([1, null, NaN, 2, undefined]);
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr.push(num);
arr.sort((a,b)=>a-b);
return arr.indexOf(num);
}
getIndexToIns([2, 5, 10], 15);
function getIndexToIns(arr, num) {
// sort and find right index
var index = arr.sort((curr, next) => curr > next)
.findIndex((currNum)=> num <= currNum);
// Returns proper answer
return index === -1 ? arr.length : index;
}
getIndexToIns([40, 60], 500);
function rot13(str) { // LBH QVQ VG!
var result="";
var zAt = "Z".charCodeAt(0);
var aAt = "A".charCodeAt(0);
for(i=0; i<str.length; i++){
if(/\W/.exec(str[i])){
result = result + str[i];
}
else if( (zAt - str.charCodeAt(i)) < 13){
result = result + String.fromCharCode(str.charCodeAt(i)-13);
}
else{
result = result + String.fromCharCode(str.charCodeAt(i)+13);
}
}
console.log(result);
return result;
}
// Change the inputs below to test
rot13("SERR CVMMN!");
function rot13(str) {
// Split str into a character array
return str.split('')
// Iterate over each character in the array
.map.call(str, function(char) {
// Convert char to a character code
x = char.charCodeAt(0);
// Checks if character lies between A-Z
if (x < 65 || x > 90) {
return String.fromCharCode(x); // Return un-converted character
}
//N = ASCII 78, if the character code is less than 78, shift forward 13 places
else if (x < 78) {
return String.fromCharCode(x + 13);
}
// Otherwise shift the character 13 places backward
return String.fromCharCode(x - 13);
}).join(''); // Rejoin the array into a string
}
// Solution with Regular expression and Array of ASCII character codes
function rot13(str) {
var rotCharArray = [];
var regEx = /[A-Z]/ ;
str = str.split("");
for (var x in str) {
if (regEx.test(str[x])) {
// A more general approach
// possible because of modular arithmetic
// and cyclic nature of rot13 transform
rotCharArray.push((str[x].charCodeAt() - 65 + 13) % 26 + 65);
} else {
rotCharArray.push(str[x].charCodeAt());
}
}
str = String.fromCharCode.apply(String, rotCharArray);
return str;
}
// Change the inputs below to test
rot13("LBH QVQ VG!");