Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js] 第133天 写一个方法,将字符串中的单词倒转后输出,如:my love -> ym evol #1118

Open
haizhilin2013 opened this issue Aug 26, 2019 · 16 comments
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

第133天 写一个方法,将字符串中的单词倒转后输出,如:my love -> ym evol

@haizhilin2013 haizhilin2013 added the js JavaScript label Aug 26, 2019
@qhdxwdm
Copy link

qhdxwdm commented Aug 26, 2019

(text)=>{ let arr= [...text]; return arr.reverse().join(''); }

@chenweihuan
Copy link

let b = "my love"
function fn(b){
	return b.split(" ").map(val => val.split("").reverse().join("")).join(" ")
}
console.log(fn(b)) //ym evol

@NicholasBaiYa
Copy link

NicholasBaiYa commented Aug 27, 2019

let wordReversal = str => 
    str
        .split(' ')
        .map(e => e.split('').reverse().join(''))
        .join(' ')

@LinStan
Copy link

LinStan commented Aug 27, 2019

let str = 'my love'
str.split(' ').map((v)=>{v.split('').reverse().join('')}).join(' ')

@Konata9
Copy link

Konata9 commented Aug 27, 2019

const reverseStr = (str) =>
  str
    ? str
        .split("")
        .reverse()
        .join("")
    : str;


console.log(reverseStr('my love'));
console.log(reverseStr('abc cba abc'));

@Clearives
Copy link

const reverseStr = str => {
  if (typeof str !== "string") throw new Error("The argument must be a string");
  return str
    .split(" ")
    .map(v =>
      v
        .split("")
        .reverse()
        .join("")
    )
    .join(" ");
};

console.log(reverseStr("my love"));
console.log(reverseStr("js upupup"));
console.log(reverseStr(123));

image

@whitesky1225
Copy link

const reverseStr = (str) =>
  str
    ? str
        .split("")
        .reverse()
        .join("")
    : str;


console.log(reverseStr('my love'));
console.log(reverseStr('abc cba abc'));

我最先想到的也是用Array.reverse()

@xxf1996
Copy link

xxf1996 commented Aug 27, 2019

function reverseStr (str) {
  return str.split('').reverse().join('')
}

@ajycc20
Copy link

ajycc20 commented Aug 27, 2019

return str.split('').reverse().join('').split(' ').reverse().join(' ')

@viccowang
Copy link

viccowang commented Aug 28, 2019

function reverseString (str) {
   if( typeof str !== "string") throw new Error('The param must be a string.')
    return str.split(' ')
      .map( splitStr => splitStr.split('').reverse().join('') )
      .join(' ')
}

console.log( reverseString('my love') )
console.log( reverseString('hello world') )
console.log( reverseString(123) )

@Shaman-HH
Copy link

function reverseString(str) {
  if (typeof str !== 'string') throw new Error('The param must be string.');
  return str.split(' ')
    .map(el => Array.from(el).reverse().join(''))
    .join(' ')
}

// test
console.log(reverseString('my love'));
console.log(reverseString('my '));
console.log(reverseString({a: 1}));
console.log(reverseString(123));

@5SSS
Copy link

5SSS commented Sep 19, 2019

能不能不用reverse🙃

function reverseString (string) {
	if (typeof string !== 'string') {
		return 'hello world!'
	}
	string = string.split('')
	let length = string.length
	for (let i = 0, len = Math.floor(length / 2); i < len; i++) {
		let t = string[i]
		string[i] = string[length - i - 1]
		string[length - i - 1] = t
	}
	return string.join('')
}

@amusiq
Copy link

amusiq commented Sep 21, 2019

(text)=>{ let arr= [...text]; return arr.reverse().join(''); }

你似乎没看清楚题目意思

@cyj1209
Copy link

cyj1209 commented Nov 13, 2019

`
function reverseString(str) {
let fast = 0;
let slow = 0;
let res = "";
for (; fast < str.length; fast++) {
if (str[fast] === " ") {
let t = "";
while (slow < fast) {
t = str[slow] + t;
slow++;
}
t += ' ';
slow++ ;
res += t;
}
}
let t = "";
while (slow < fast) {
t = str[slow] + t;
slow++;
}
res += t;
return res;
}

console.log(reverseString("my love"));
`
双指针扫描字符串

@1684838553
Copy link

function reverseStr(str){
    return str.split(' ').map(item => item.split('').reverse().join('')).join(' ')
}

@xiaoqiangz
Copy link

str1.split(' ').map(item => item.split('').reverse().join('')).join(' ')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests