首页 > 其他分享 >给定特定的字符串,写个方法判断是否以元字母结尾

给定特定的字符串,写个方法判断是否以元字母结尾

时间:2024-12-10 09:10:50浏览次数:5  
标签:写个 console 结尾 endsWithVowel vowels 以元 str false log

function endsWithVowel(str) {
  if (!str || str.length === 0) {
    return false; // Or handle empty strings as needed
  }

  const vowels = "aeiouAEIOU";
  const lastChar = str.charAt(str.length - 1);
  return vowels.includes(lastChar);
}


// Examples
console.log(endsWithVowel("hello")); // true
console.log(endsWithVowel("world")); // false
console.log(endsWithVowel("APPLE")); // true
console.log(endsWithVowel(""));      // false (or handle as needed)
console.log(endsWithVowel(null));   // false (or handle as needed)
console.log(endsWithVowel(undefined)); // false (or handle as needed)


// More robust handling of null/undefined/empty strings:
function endsWithVowelRobust(str) {
    if (typeof str !== 'string' || !str) {  // Check for non-string or empty/null/undefined
        return false;
    }
    const vowels = "aeiouAEIOU";
    const lastChar = str.charAt(str.length - 1);
    return vowels.includes(lastChar);
}

Explanation and Improvements:

  1. Handle Empty/Null/Undefined: The original version could throw errors if str was null, undefined, or an empty string. The endsWithVowelRobust function explicitly checks for these cases and returns false. You can adjust the return value to suit your specific needs.

  2. Clearer Variable Names: Using vowels instead of just v makes the code more readable.

  3. Case-Insensitive: The vowels string includes both uppercase and lowercase vowels, making the function case-insensitive.

  4. includes() Method: Using vowels.includes(lastChar) is a concise and efficient way to check if the last character is a vowel.

  5. Comments and Examples: Adding comments and examples makes the code easier to understand and test.

Alternative using Regular Expression:

function endsWithVowelRegex(str) {
  if (typeof str !== 'string' || !str) {
    return false;
  }
  return /[aeiouAEIOU]$/.test(str);
}

// Examples (same results as above)
console.log(endsWithVowelRegex("hello")); // true
console.log(endsWithVowelRegex("world")); // false
console.log(endsWithVowelRegex("APPLE")); // true
console.log(endsWithVowelRegex(""));      // false
console.log(endsWithVowelRegex(null));   // false
console.log(endsWithVowelRegex(undefined)); // false

The regular expression /[aeiouAEIOU]$/ checks if the string ends ($) with any of the characters within the square brackets (the vowels). This is a very compact way to achieve the same result.

Choose the method that you find most readable and maintainable for your project. The includes() version is often slightly faster for simple vowel checks, while the regular expression approach can be more flexible if you need to handle more complex patterns in the future.

标签:写个,console,结尾,endsWithVowel,vowels,以元,str,false,log
From: https://www.cnblogs.com/ai888/p/18596532

相关文章

  • 如果我想写个filter过滤器
    JavaWeb过滤器(Filter)详解,是时候该把过滤器彻底搞懂了(万字说明)_webfilter-CSDN博客 1.配置Filter的拦截路径有2种方式,一种是注解,一种是xml方式,注解方式我们如果使用注解来进行配置,那么我们就需要使用@WebFilterfilterName:该filter的名字initParams:初始化参数displayName:fi......
  • 写个布局,当页面高度不够时,底部固定在下面,反之不固定
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>StickyFooter</title>......
  • 如何让大小不同的图片等比缩放不变形显示在固定大小的div里?写个例子
    要让大小不同的图片等比缩放不变形显示在固定大小的div里,关键在于设置object-fitCSS属性。以下是一个例子,并解释了不同的object-fit值的效果:<!DOCTYPEhtml><html><head><title>ImageScaling</title><style>.container{width:200px;height:200px;border......
  • 写个方法随机打乱一个数组
    functionshuffleArray(array){//创建数组的副本,避免修改原始数组constshuffledArray=[...array];//Fisher-Yates洗牌算法for(leti=shuffledArray.length-1;i>0;i--){constj=Math.floor(Math.random()*(i+1));//随机索引0到i......
  • 用markdown写个博客网站吧
    背景你看到别人都有自己的个人博客,你不想也搞一个吗?作者的博客示例:博客演示地址源码地址:码云技术与环境vitePress:官网markdownvuenodejs:V18.0+yarn用来安装依赖,也可以用pnpm、bun、npm等开始搭建1.建一个空的文件夹,如:testBlog2.安装vitepress:yarnadd-Dvitepres......
  • 画个心,写个花!Python Turtle库带你玩转创意绘图!
    文章目录前言一、Turtle库基础介绍二、画布设置三、画笔属性设置1.画笔颜色设置2.画笔粗细与速度设置3.画笔形状设置四、画笔移动函数五、画笔控制函数六、实战案例一:“花”字绘制七、实战案例二:心型图案绘制总结前言Python的turtle库是一种简单易用的绘图工具......
  • Code-server写个C#
    安装.NETSDKsudoaptupdatesudoaptinstallwgetwgethttps://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb-Opackages-microsoft-prod.debsudodpkg-ipackages-microsoft-prod.debsudoaptinstalldotnet-sdk-6.0创建C#项目在code-s......
  • 黑马面试集合(ArrayList, HashMap)篇笔记整理,结尾附Java的集合相关高频面试题及答案
    集合操作数据的特点-算法复杂度分析数据结构算法复杂度分析为什么要进行复杂度分析?指导编写性能更优的代码评判别人写代码的好坏时间复杂度分析时间复杂度分析:来评估代码的执行耗时的假设每行代码的执行耗时一样:1ms分析这段代码一共执行多少行?3n+3......
  • 短视频上传怎么做|写个支持分片上传/断点续传/秒传功能的文件服务吧
    前言各位平时使用的短视频应用,微信&微博等图文社区,它们的图文动态&视频上传的能力,都是极其核心的业务。本质来说,这都是文件的上传,这篇文章带大家写一个文件上传服务,探究其核心原理,相信能为你带来一些帮助。感谢我的好友Trembling对本文的支持主要包含以下能力:文件上......
  • Python科研武器库 - 字符串操作 - 字符串开头结尾判断 startswith() endswith()
    使用场景:按照开头的前缀、结尾的后缀来判断、筛选目标字符串。使用函数:str.startswith(search_string,start,end)str.endswith(search_string,start,end)search_string:要匹配的目标字符串。start:要从中匹配search_string的str的起始索引。end:要考虑匹配的str的结......