FCC_Intermediate Algorithm Scripting_Search and Replace

1.任务及要求

Search and Replace


使用给定的参数对句子执行一次查找和替换,然后返回新句子。

第一个参数是将要对其执行查找和替换的句子。

第二个参数是将被替换掉的单词(替换前的单词)。

第三个参数用于替换第二个参数(替换后的单词)。

注意:替换时保持原单词的大小写。例如,如果你想用单词 "dog" 替换单词 "Book" ,你应该替换成 "Dog"。

如果你被难住了,记得使用 Read-Search-Ask尝试与他人结伴编程、编写你自己的代码。

这是一些对你有帮助的资源:

测试数据:

 myReplace("Let us go to the store", "store", "mall") 应该返回 "Let us go to the mall"。

 

 myReplace("He is Sleeping on the couch", "Sleeping", "sitting") 应该返回 "He is Sitting on the couch"。

 myReplace("This has a spellngi error", "spellngi", "spelling") 应该返回 "This has a spelling error"。

 myReplace("His name is Tom", "Tom", "john") 应该返回 "His name is John"。

 myReplace("Let us get back to more Coding", "Coding", "algorithms") 应该返回 "Let us get back to more Algorithms"。

2.我的解法

function myReplace(str, before, after) {
 // 字符串首字母大写方法参考:
 // https://www.cnblogs.com/ToBeBest/p/9724955.html

  if(before.charAt(0)>‘A‘ && before.charAt(0) <‘Z‘) {
    after = after.slice(0,1).toUpperCase() + after.slice(1);
  }

  return str.replace(before,after);
}

myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

原文地址:https://www.cnblogs.com/yoursatan/p/12402080.html

时间: 2024-10-22 05:47:25

FCC_Intermediate Algorithm Scripting_Search and Replace的相关文章

FCC_Intermediate Algorithm Scripting_Where art thou

1.任务及要求 Where art thou 写一个 function,它遍历一个对象数组(第一个参数)并返回一个包含相匹配的属性-值对(第二个参数)的所有对象的数组.如果返回的数组中包含 source 对象的属性-值对,那么此对象的每一个属性-值对都必须存在于 collection 的对象中. 例如,如果第一个参数是 [{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last

FCC_Intermediate Algorithm Scripting_Diff Two Arrays

1.任务及要求 Diff Two Arrays 比较两个数组,然后返回一个新数组,该数组的元素为两个给定数组中所有独有的数组元素.换言之,返回两个数组的差异. 如果你被难住了,记得使用 Read-Search-Ask尝试与他人结伴编程.编写你自己的代码. 这是一些对你有帮助的资源: Comparison Operators Array.slice() Array.filter() Array.indexOf() Array.concat() 测试数据: diff([1, 2, 3, 5], [1

FCC_Intermediate Algorithm Scripting_Sum All Numbers in a Range

1.任务及要求 Sum All Numbers in a Range 我们会传递给你一个包含两个数字的数组.返回这两个数字和它们之间所有数字的和. 最小的数字并非总在最前面. 如果你被难住了,记得使用 Read-Search-Ask.尝试与他人结伴编程.编写你自己的代码. 这是一些对你有帮助的资源: Math.max() Math.min() Array.reduce() 测试数据:  sumAll([1, 4]) 应该返回一个数字. sumAll([1, 4]) 应该返回 10. sumAll

Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency_Pseudocode

This pseudocode from the book: <<Introduction to the Design and Analysis of Algorithms_Second Edition>> _ Anany LevitinNote that throughout the paper, we assume that inputs to algorithms fall within their specified ranges and hence require no

HDU-4288-Coder(线段树)

Problem Description In mathematics and computer science, an algorithm describes a set of procedures or instructions that define a procedure. The term has become increasing popular since the advent of cheap and reliable computers. Many companies now e

杭电 HDU ACM Coder (STL)

欢迎参加--每周六晚的BestCoder(有米!) Coder Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4200    Accepted Submission(s): 1630 Problem Description In mathematics and computer science, an algorithm desc

hdu4288 离线处理线段树

http://acm.hdu.edu.cn/showproblem.php?pid=4288 Problem Description In mathematics and computer science, an algorithm describes a set of procedures or instructions that define a procedure. The term has become increasing popular since the advent of che

HDU 4288-Coder(模拟)

Coder Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3327    Accepted Submission(s): 1316 Problem Description In mathematics and computer science, an algorithm describes a set of procedures

hdu 4288 Coder(树形结构-线段树)

<span style="font-family: 'Times New Roman'; font-size: 12px;">Coder</span> Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3187    Accepted Submission(s): 1258 Problem Descr