Javascript 我试图减去两个字符串并返回数组中的单词
我正在尝试用JavaScript编写一个程序,下面是给定的代码。我的函数需要接受两个字符串(s,t),其中s是完整字符串,t是序列 例如:Javascript 我试图减去两个字符串并返回数组中的单词,javascript,string,Javascript,String,我正在尝试用JavaScript编写一个程序,下面是给定的代码。我的函数需要接受两个字符串(s,t),其中s是完整字符串,t是序列 例如: s=“我爱嗡嗡世界中的月亮” t=“我爱月亮世界” 输出:[“the”,“in”,“humming”] 我尝试了类似的方法,但无法理解为什么我的if条件第一次没有运行,但代码运行良好。有人能帮忙吗?请注意,需要按顺序搜索的序列不能向后搜索,因此每次搜索都将从找到最后一个元素的位置继续 代码: 功能缺失字(s,t) { 风险价值指数=0; var full
s=“我爱嗡嗡世界中的月亮”
t=“我爱月亮世界”
[“the”,“in”,“humming”]
我尝试了类似的方法,但无法理解为什么我的if条件第一次没有运行,但代码运行良好。有人能帮忙吗?请注意,需要按顺序搜索的序列不能向后搜索,因此每次搜索都将从找到最后一个元素的位置继续
代码:
功能缺失字(s,t)
{
风险价值指数=0;
var full_string_array=s.split(“”);
var序列_数组=t.split(“”);
var数组返回=s.split(“”);
变量搜索数组=s.split(“”);
对于(var i=0;i>
5.
0
5.
0
[‘我’、‘你’、‘在’、‘月亮’、‘of’、‘哼唱’]
5.
0
[‘我’、‘在’、‘月亮’、‘的’、‘哼唱’]
5.
0
[‘我’、‘在’、‘月亮’、‘的’、‘哼唱’]
5.
1.
[‘我’、‘在’、‘月亮’、‘哼唱’]
[‘我’、‘在’、‘月亮’、‘哼唱’]
**所需输出**>>
没有我[“in”,“moon”,“humming”]
您只需使用映射器对象和过滤器即可
t
字符串中的单词映射为关键字和引用值s
并基于映射器对象过滤值,如果映射器对象上存在该值且该值大于0,则将其值减少1并返回false,否则返回truefunction missingWords(s, t)
{
var found_index = 0;
var full_string_array = s.split(" ");
var sequence_array = t.split(" ");
var array_to_return = s.split(" ");
var searching_array = s.split(" ");
for (var i= 0 ; i < sequence_array.length ; i++)
{
#just trying to debug the code by two lines below
console.log(sequence_array.length)
console.log(found_index)
if((array_to_return.indexOf(sequence_array[i], found_index)))
{
found_index = array_to_return.indexOf(sequence_array[i], found_index);
array_to_return.splice(found_index, 1)
found_index = found_index-1;
console.log(array_to_return);
}
}
return array_to_return;
}
console.log(missingWords("i love u in the moon of humming","i love u the of"));```
output >>
5
0
5
0
[ 'i', 'u', 'in', 'the', 'moon', 'of', 'humming' ]
5
0
[ 'i', 'in', 'the', 'moon', 'of', 'humming' ]
5
0
[ 'i', 'in', 'moon', 'of', 'humming' ]
5
1
[ 'i', 'in', 'moon', 'humming' ]
[ 'i', 'in', 'moon', 'humming' ]
**desired output**>>
without i ["in","moon","humming"]