codewars练习(javascript)-2021/2/5

本文介绍了三个JavaScript编程挑战,包括使用正则表达式验证密码,实现两数之和的查找以及字符串的递增功能。每个问题都有详细的解题思路和代码实现,并附带了测试用例进行验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

codewars-js练习

2021/2/5

github 地址

my github地址,上面有做的习题记录,不断更新…

【1】<5kyu>【Regex Password Validation】

You need to write regex that will validate a password to make sure it meets the following criteria:

  • At least six characters long
  • contains a lowercase letter
  • contains an uppercase letter
  • contains a number

Valid passwords will only be alphanumeric characters.

您需要编写正则表达式来验证密码,以确保它符合以下条件:

至少6个字符/包含小写字母/包含大写字母/包含许多/有效的密码只能是字母数字字符。

example

('djI38D55'), 'djI38D55 - Expected true'
('a2.d412'), 'a2.d412 - Expected false'

solution

<script type="text/javascript">
 		function validate(password) {
 	 		return /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])\w{6,}$/g.test(password);
		}

 
 		// 验证
		console.log(validate('djI38D55')); // true
		console.log(validate('a2.d412')); //false
		console.log(validate('123'));//false
		console.log(validate('jfkdfj3j'));//false
		console.log(validate('JHD5FJ53'));//false
	</script>
【2】<6kyu>【Two Sum】

Write a function that takes an array of numbers (integers for the tests) and a target number. It should find two different items in the array that, when added together, give the target value. The indices of these items should then be returned in a tuple like so: (index1, index2).

返回相加等于目标和的两个index

example

twoSum [1, 2, 3] 4 //(0, 2)
twoSum [1234,5678,9012] 14690//[1,2]

solution

<script type="text/javascript">
 		function twoSum(numbers, target) {
 			// console.log(numbers,target);
 			for(var i=0;i<numbers.length;i++){
 				for(var j=i+1;j<numbers.length;j++){
 					if(numbers[i] + numbers[j] == target){
 						// console.log(numbers[i]+numbers[j] == target);
 						return [i,j];
 					}
 				}			
 			}
 		}
 
 		// 验证
		console.log(twoSum([1,2,3],4)); // [0,2]
		console.log(twoSum([1234,5678,9012],14690));//[1,2])
	</script>
【3】<5kyu>【String incrementer】

Your job is to write a function which increments a string, to create a new string.

  • If the string already ends with a number, the number should be incremented by 1.
  • If the string does not end with a number. the number 1 should be appended to the new string.

你的工作是写一个函数来增加一个字符串,来创建一个新的字符串。

如果字符串已经以一个数字结束,则该数字应该加1;如果字符串没有以数字结束。数字1应该被附加到新的字符串中。

example

foo -> foo1

foobar23 -> foobar24

foo0042 -> foo0043

foo9 -> foo10

foo099 -> foo100

solution

<script type="text/javascript">
 		function incrementString (strng) {
 			console.log(strng);
 			var arr = strng.split('');
 			var num = strng.match(/(\d*)$/)[0];
 			// console.log(typeof num);
 			var len = strng.length - num.length;
 			if(num !=0){
 				for(var i=num.length-1;i>0;i--){
 					if(parseInt(num[i])==0){
 						// console.log(strng.substring(0,len+i+1));
 						// console.log(strng.substring(strng.length-1,strng.length))
 						if(strng.substring(strng.length-1,strng.length) == 9){
 							return strng.substring(0,len+i) + (parseInt(num)+1);
 						}
 						return strng.substring(0,len+i+1) + (parseInt(num)+1);
 						

 						// if()
 					}
 				}
 				return strng.substring(0,len) + (parseInt(num)+1);
 			}else{
 				console.log((/0{1,}/g).test(num))
 				// console.log('2')
 				if((/0{1,}/g).test(num)){
 					for(var i=num.length-1;i>0;i--){
 					if(parseInt(num[i])==0){
 						return strng.substring(0,len+i) + (parseInt(num)+1);
 					}
 				}
 				}else{
 					return strng + '1';
 				}
 				
 			}
 			
 		}
 
 	 	// 验证
		console.log(incrementString('foo')); // foo1
		// console.log(incrementString('foo9'));// foo10
		// console.log(incrementString('foobar001'));// foobar002
		console.log(incrementString('foobar000'))//foobar001

	</script>

以上为自己思路供大家参考,可能有更优的思路。最后一道祈求最优解,感觉我写的很繁琐

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值