Creating a wave of string in Javascript2019 Community Moderator ElectionHow do I replace a character at a...
Are there any other Chaos worshipping races?
Non-Italian European mafias in USA?
If nine coins are tossed, what is the probability that the number of heads is even?
Are small insurances worth it
What type of postprocessing gives the effect of people standing out
Canadian citizen, on US no-fly list. What can I do in order to be allowed on flights which go through US airspace?
I can't die. Who am I?
Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?
Difference between 'stomach' and 'uterus'
Is it possible to keep the Ring of Winter if you manage to acquire it?
How to deal with being jealous of your own players?
How would we write a misogynistic character without offending people?
Why can't we make a perpetual motion machine by using a magnet to pull up a piece of metal, then letting it fall back down?
Wiring up text parts
What does 'acting greedily' mean?
Why is working on the same position for more than 15 years not a red flag?
All possible A of Ax=b with constraints on A
Does Garmin Oregon 700 have Strava integration?
Is the set of paths between any two points moving only in units on the plane countable or uncountable?
How can I be pwned if I'm not registered on the compromised site?
Get length of the longest sequence of numbers with the same sign
How do ISS astronauts "get their stripes"?
Plagiarism of code by other PhD student
What is this waxed root vegetable?
Creating a wave of string in Javascript
2019 Community Moderator ElectionHow do I replace a character at a particular index in JavaScript?Why is extending native objects a bad practice?Create GUID / UUID in JavaScript?How do JavaScript closures work?Which equals operator (== vs ===) should be used in JavaScript comparisons?Creating multiline strings in JavaScriptHow do I include a JavaScript file in another JavaScript file?How to replace all occurrences of a string in JavaScriptWhat does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
I can't seem to figure it out how to make a wave from a string in Javascript.
Rules:
- The input will always be lower case string.
- Ignore whitespace.
Expected result:
wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
wave (" h e y ") => [" H e y ", " h E y ", " h e Y "]
wave ("") => []
This is as far as I got. Current code will give me an answer ["hello", "hello", "hello", "hello", "hello"]
. I'm thinking using second for loop and somehow capitalize each new letter but I'am stumped. Also I would appreciate if answer would avoid using loop inside loop O(n^2)
. Because of BIG O Scalability.
const wave = (str) => {
if(typeof str === 'string' && str === str.toLowerCase()){
for (let index = 0; index < str.length; index++) {
array.push(str);
}
for (let index = 0; index < str.length; index++) {
console.log(array);
}
}else{
alert(`${str} is either not a string or not lowercase`);
}
}
wave("hello");
javascript arrays
add a comment |
I can't seem to figure it out how to make a wave from a string in Javascript.
Rules:
- The input will always be lower case string.
- Ignore whitespace.
Expected result:
wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
wave (" h e y ") => [" H e y ", " h E y ", " h e Y "]
wave ("") => []
This is as far as I got. Current code will give me an answer ["hello", "hello", "hello", "hello", "hello"]
. I'm thinking using second for loop and somehow capitalize each new letter but I'am stumped. Also I would appreciate if answer would avoid using loop inside loop O(n^2)
. Because of BIG O Scalability.
const wave = (str) => {
if(typeof str === 'string' && str === str.toLowerCase()){
for (let index = 0; index < str.length; index++) {
array.push(str);
}
for (let index = 0; index < str.length; index++) {
console.log(array);
}
}else{
alert(`${str} is either not a string or not lowercase`);
}
}
wave("hello");
javascript arrays
Shouldn't you convert some characters to upper case?
– Aycan Yaşıt
yesterday
add a comment |
I can't seem to figure it out how to make a wave from a string in Javascript.
Rules:
- The input will always be lower case string.
- Ignore whitespace.
Expected result:
wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
wave (" h e y ") => [" H e y ", " h E y ", " h e Y "]
wave ("") => []
This is as far as I got. Current code will give me an answer ["hello", "hello", "hello", "hello", "hello"]
. I'm thinking using second for loop and somehow capitalize each new letter but I'am stumped. Also I would appreciate if answer would avoid using loop inside loop O(n^2)
. Because of BIG O Scalability.
const wave = (str) => {
if(typeof str === 'string' && str === str.toLowerCase()){
for (let index = 0; index < str.length; index++) {
array.push(str);
}
for (let index = 0; index < str.length; index++) {
console.log(array);
}
}else{
alert(`${str} is either not a string or not lowercase`);
}
}
wave("hello");
javascript arrays
I can't seem to figure it out how to make a wave from a string in Javascript.
Rules:
- The input will always be lower case string.
- Ignore whitespace.
Expected result:
wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
wave (" h e y ") => [" H e y ", " h E y ", " h e Y "]
wave ("") => []
This is as far as I got. Current code will give me an answer ["hello", "hello", "hello", "hello", "hello"]
. I'm thinking using second for loop and somehow capitalize each new letter but I'am stumped. Also I would appreciate if answer would avoid using loop inside loop O(n^2)
. Because of BIG O Scalability.
const wave = (str) => {
if(typeof str === 'string' && str === str.toLowerCase()){
for (let index = 0; index < str.length; index++) {
array.push(str);
}
for (let index = 0; index < str.length; index++) {
console.log(array);
}
}else{
alert(`${str} is either not a string or not lowercase`);
}
}
wave("hello");
javascript arrays
javascript arrays
edited yesterday
vahdet
1,99131430
1,99131430
asked yesterday
Arnas DičkusArnas Dičkus
7518
7518
Shouldn't you convert some characters to upper case?
– Aycan Yaşıt
yesterday
add a comment |
Shouldn't you convert some characters to upper case?
– Aycan Yaşıt
yesterday
Shouldn't you convert some characters to upper case?
– Aycan Yaşıt
yesterday
Shouldn't you convert some characters to upper case?
– Aycan Yaşıt
yesterday
add a comment |
5 Answers
5
active
oldest
votes
You could take an outer loop for visiting the characters and if a non space character is found, create a new string with an uppercase letter at this position.
function wave(string) {
var result = [],
i;
for (i = 0; i < string.length; i++) {
if (string[i] === ' ') continue;
result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join(''));
}
return result;
}
console.log(wave("hello")); // ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
console.log(wave(" h e y ")); // [" H e y ", " h E y ", " h e Y "]
console.log(wave("")); // []
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
This does it. However, if you have spaces in your string, it will output string without any "waved letter" (since also space is handled):
const wave = (str = '') => {
return str
.split('')
.map((letter, i, arr) => `${arr.slice(0, i)}${letter.toUpperCase()}${arr.slice(i + 1, arr.length)}`.replace(/,/g, ''));
}
console.log(wave('wave'));
console.log(wave('foo bar'));
console.log(wave());
add a comment |
You can take each char in string in for loop and make it uppercase and the append with prefix and post fix string
var array=[];
const wave = (str) => {
if(typeof str === 'string' && str === str.toLowerCase()){
for (let index = 0; index < str.length; index++) {
if (str[index] === ' ') continue;
waveChar=str.charAt(index).toUpperCase();
preStr=str.substring(0,index);
postStr=str.substring(index,str.length-1);
array.push(preStr+waveChar+postStr);
}
}else{
alert(`${str} is either not a string or not lowercase`);
}
}
wave("hello");
console.log(array);
add a comment |
I use tradicional js. This works on 99% off today browsers.
Where answer very pragmatic. I use array access for string ;
Magic is "String.fromCharCode(str.charCodeAt(x) ^ 32);"
Make it inverse always when we call this line.
var index = 0;
var mydata= "hello";
function wave (str){
var FINAL = [];
var newString = "";
for (var j =0; j < str.length; j++) {
for (var x =0; x < str.length; x++) {
var rez = "";
if (x == index) {
rez = String.fromCharCode(str.charCodeAt(x) ^ 32);
} else {
rez = str[x];
}
newString += rez ;
}
FINAL.push(newString);
newString = "";
index++;
}
return FINAL;
}
var rezArray = wave(mydata);
console.log(rezArray);
// Just for teory
console.log(Array.from([1, 2, 3], x => console.log(x)));
Your answer uses 2 for loops inside each other. Which is Bad for Scalability O(n^2). According to BIG O notations.BIG O Cheatsheet
– Arnas Dičkus
yesterday
You must understand that " for (i = 0; i < string.length; i++) { if (string[i] === ' ') continue; result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); }" is ALSO loop in twice. ;) When you call this line : Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); you work with iterator.
– Nikola Lukic
yesterday
What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));
– Nikola Lukic
yesterday
1
Thanks, for explanation I wasn't aware of this.
– Arnas Dičkus
yesterday
2
"String.fromCharCode(str.charCodeAt(x) ^ 32)
" is too much magic. It's a nice trick, but works only on ASCII characters. Don't do that. There's no reason not to use.toUpperCase()
.
– Bergi
23 hours ago
|
show 3 more comments
I use modify of String prototype :
for implementation of replaceAt .
// Modify prototype
String.prototype.replaceAt=function(index, replacement) {
return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
}
function WaveFunction(str) {
var base = str;
var R = [];
var n =str.length;
for (var i=0 ; i<n ;i++){
str = base;
var c=str.charAt(i);
var res = c.toUpperCase();
// console.log(res);
str = str.replaceAt(i, res);
R.push(str);
}
return R;
}
var REZ = WaveFunction("hello");
console.log(REZ);
1
Nice solution with replaceAt !
– Nikola Lukic
yesterday
where isreplaceAt
from?
– Nina Scholz
yesterday
@NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.
– Arnas Dičkus
yesterday
1
String.prototype.replaceAt=function(index, replacement) { return this.substr(0, index) + replacement+ this.substr(index + replacement.length); } I am sorry i forgot to add it to my answer :)
– Peter Hassaballah
yesterday
1
Don't modify objects you don't own.
– Emile Bergeron
yesterday
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54983179%2fcreating-a-wave-of-string-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could take an outer loop for visiting the characters and if a non space character is found, create a new string with an uppercase letter at this position.
function wave(string) {
var result = [],
i;
for (i = 0; i < string.length; i++) {
if (string[i] === ' ') continue;
result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join(''));
}
return result;
}
console.log(wave("hello")); // ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
console.log(wave(" h e y ")); // [" H e y ", " h E y ", " h e Y "]
console.log(wave("")); // []
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
You could take an outer loop for visiting the characters and if a non space character is found, create a new string with an uppercase letter at this position.
function wave(string) {
var result = [],
i;
for (i = 0; i < string.length; i++) {
if (string[i] === ' ') continue;
result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join(''));
}
return result;
}
console.log(wave("hello")); // ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
console.log(wave(" h e y ")); // [" H e y ", " h E y ", " h e Y "]
console.log(wave("")); // []
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
You could take an outer loop for visiting the characters and if a non space character is found, create a new string with an uppercase letter at this position.
function wave(string) {
var result = [],
i;
for (i = 0; i < string.length; i++) {
if (string[i] === ' ') continue;
result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join(''));
}
return result;
}
console.log(wave("hello")); // ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
console.log(wave(" h e y ")); // [" H e y ", " h E y ", " h e Y "]
console.log(wave("")); // []
.as-console-wrapper { max-height: 100% !important; top: 0; }
You could take an outer loop for visiting the characters and if a non space character is found, create a new string with an uppercase letter at this position.
function wave(string) {
var result = [],
i;
for (i = 0; i < string.length; i++) {
if (string[i] === ' ') continue;
result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join(''));
}
return result;
}
console.log(wave("hello")); // ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
console.log(wave(" h e y ")); // [" H e y ", " h E y ", " h e Y "]
console.log(wave("")); // []
.as-console-wrapper { max-height: 100% !important; top: 0; }
function wave(string) {
var result = [],
i;
for (i = 0; i < string.length; i++) {
if (string[i] === ' ') continue;
result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join(''));
}
return result;
}
console.log(wave("hello")); // ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
console.log(wave(" h e y ")); // [" H e y ", " h E y ", " h e Y "]
console.log(wave("")); // []
.as-console-wrapper { max-height: 100% !important; top: 0; }
function wave(string) {
var result = [],
i;
for (i = 0; i < string.length; i++) {
if (string[i] === ' ') continue;
result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join(''));
}
return result;
}
console.log(wave("hello")); // ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
console.log(wave(" h e y ")); // [" H e y ", " h E y ", " h e Y "]
console.log(wave("")); // []
.as-console-wrapper { max-height: 100% !important; top: 0; }
answered yesterday
Nina ScholzNina Scholz
189k1598173
189k1598173
add a comment |
add a comment |
This does it. However, if you have spaces in your string, it will output string without any "waved letter" (since also space is handled):
const wave = (str = '') => {
return str
.split('')
.map((letter, i, arr) => `${arr.slice(0, i)}${letter.toUpperCase()}${arr.slice(i + 1, arr.length)}`.replace(/,/g, ''));
}
console.log(wave('wave'));
console.log(wave('foo bar'));
console.log(wave());
add a comment |
This does it. However, if you have spaces in your string, it will output string without any "waved letter" (since also space is handled):
const wave = (str = '') => {
return str
.split('')
.map((letter, i, arr) => `${arr.slice(0, i)}${letter.toUpperCase()}${arr.slice(i + 1, arr.length)}`.replace(/,/g, ''));
}
console.log(wave('wave'));
console.log(wave('foo bar'));
console.log(wave());
add a comment |
This does it. However, if you have spaces in your string, it will output string without any "waved letter" (since also space is handled):
const wave = (str = '') => {
return str
.split('')
.map((letter, i, arr) => `${arr.slice(0, i)}${letter.toUpperCase()}${arr.slice(i + 1, arr.length)}`.replace(/,/g, ''));
}
console.log(wave('wave'));
console.log(wave('foo bar'));
console.log(wave());
This does it. However, if you have spaces in your string, it will output string without any "waved letter" (since also space is handled):
const wave = (str = '') => {
return str
.split('')
.map((letter, i, arr) => `${arr.slice(0, i)}${letter.toUpperCase()}${arr.slice(i + 1, arr.length)}`.replace(/,/g, ''));
}
console.log(wave('wave'));
console.log(wave('foo bar'));
console.log(wave());
const wave = (str = '') => {
return str
.split('')
.map((letter, i, arr) => `${arr.slice(0, i)}${letter.toUpperCase()}${arr.slice(i + 1, arr.length)}`.replace(/,/g, ''));
}
console.log(wave('wave'));
console.log(wave('foo bar'));
console.log(wave());
const wave = (str = '') => {
return str
.split('')
.map((letter, i, arr) => `${arr.slice(0, i)}${letter.toUpperCase()}${arr.slice(i + 1, arr.length)}`.replace(/,/g, ''));
}
console.log(wave('wave'));
console.log(wave('foo bar'));
console.log(wave());
answered yesterday
zvonazvona
12.6k14059
12.6k14059
add a comment |
add a comment |
You can take each char in string in for loop and make it uppercase and the append with prefix and post fix string
var array=[];
const wave = (str) => {
if(typeof str === 'string' && str === str.toLowerCase()){
for (let index = 0; index < str.length; index++) {
if (str[index] === ' ') continue;
waveChar=str.charAt(index).toUpperCase();
preStr=str.substring(0,index);
postStr=str.substring(index,str.length-1);
array.push(preStr+waveChar+postStr);
}
}else{
alert(`${str} is either not a string or not lowercase`);
}
}
wave("hello");
console.log(array);
add a comment |
You can take each char in string in for loop and make it uppercase and the append with prefix and post fix string
var array=[];
const wave = (str) => {
if(typeof str === 'string' && str === str.toLowerCase()){
for (let index = 0; index < str.length; index++) {
if (str[index] === ' ') continue;
waveChar=str.charAt(index).toUpperCase();
preStr=str.substring(0,index);
postStr=str.substring(index,str.length-1);
array.push(preStr+waveChar+postStr);
}
}else{
alert(`${str} is either not a string or not lowercase`);
}
}
wave("hello");
console.log(array);
add a comment |
You can take each char in string in for loop and make it uppercase and the append with prefix and post fix string
var array=[];
const wave = (str) => {
if(typeof str === 'string' && str === str.toLowerCase()){
for (let index = 0; index < str.length; index++) {
if (str[index] === ' ') continue;
waveChar=str.charAt(index).toUpperCase();
preStr=str.substring(0,index);
postStr=str.substring(index,str.length-1);
array.push(preStr+waveChar+postStr);
}
}else{
alert(`${str} is either not a string or not lowercase`);
}
}
wave("hello");
console.log(array);
You can take each char in string in for loop and make it uppercase and the append with prefix and post fix string
var array=[];
const wave = (str) => {
if(typeof str === 'string' && str === str.toLowerCase()){
for (let index = 0; index < str.length; index++) {
if (str[index] === ' ') continue;
waveChar=str.charAt(index).toUpperCase();
preStr=str.substring(0,index);
postStr=str.substring(index,str.length-1);
array.push(preStr+waveChar+postStr);
}
}else{
alert(`${str} is either not a string or not lowercase`);
}
}
wave("hello");
console.log(array);
var array=[];
const wave = (str) => {
if(typeof str === 'string' && str === str.toLowerCase()){
for (let index = 0; index < str.length; index++) {
if (str[index] === ' ') continue;
waveChar=str.charAt(index).toUpperCase();
preStr=str.substring(0,index);
postStr=str.substring(index,str.length-1);
array.push(preStr+waveChar+postStr);
}
}else{
alert(`${str} is either not a string or not lowercase`);
}
}
wave("hello");
console.log(array);
var array=[];
const wave = (str) => {
if(typeof str === 'string' && str === str.toLowerCase()){
for (let index = 0; index < str.length; index++) {
if (str[index] === ' ') continue;
waveChar=str.charAt(index).toUpperCase();
preStr=str.substring(0,index);
postStr=str.substring(index,str.length-1);
array.push(preStr+waveChar+postStr);
}
}else{
alert(`${str} is either not a string or not lowercase`);
}
}
wave("hello");
console.log(array);
answered yesterday
Code_ModeCode_Mode
1,192715
1,192715
add a comment |
add a comment |
I use tradicional js. This works on 99% off today browsers.
Where answer very pragmatic. I use array access for string ;
Magic is "String.fromCharCode(str.charCodeAt(x) ^ 32);"
Make it inverse always when we call this line.
var index = 0;
var mydata= "hello";
function wave (str){
var FINAL = [];
var newString = "";
for (var j =0; j < str.length; j++) {
for (var x =0; x < str.length; x++) {
var rez = "";
if (x == index) {
rez = String.fromCharCode(str.charCodeAt(x) ^ 32);
} else {
rez = str[x];
}
newString += rez ;
}
FINAL.push(newString);
newString = "";
index++;
}
return FINAL;
}
var rezArray = wave(mydata);
console.log(rezArray);
// Just for teory
console.log(Array.from([1, 2, 3], x => console.log(x)));
Your answer uses 2 for loops inside each other. Which is Bad for Scalability O(n^2). According to BIG O notations.BIG O Cheatsheet
– Arnas Dičkus
yesterday
You must understand that " for (i = 0; i < string.length; i++) { if (string[i] === ' ') continue; result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); }" is ALSO loop in twice. ;) When you call this line : Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); you work with iterator.
– Nikola Lukic
yesterday
What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));
– Nikola Lukic
yesterday
1
Thanks, for explanation I wasn't aware of this.
– Arnas Dičkus
yesterday
2
"String.fromCharCode(str.charCodeAt(x) ^ 32)
" is too much magic. It's a nice trick, but works only on ASCII characters. Don't do that. There's no reason not to use.toUpperCase()
.
– Bergi
23 hours ago
|
show 3 more comments
I use tradicional js. This works on 99% off today browsers.
Where answer very pragmatic. I use array access for string ;
Magic is "String.fromCharCode(str.charCodeAt(x) ^ 32);"
Make it inverse always when we call this line.
var index = 0;
var mydata= "hello";
function wave (str){
var FINAL = [];
var newString = "";
for (var j =0; j < str.length; j++) {
for (var x =0; x < str.length; x++) {
var rez = "";
if (x == index) {
rez = String.fromCharCode(str.charCodeAt(x) ^ 32);
} else {
rez = str[x];
}
newString += rez ;
}
FINAL.push(newString);
newString = "";
index++;
}
return FINAL;
}
var rezArray = wave(mydata);
console.log(rezArray);
// Just for teory
console.log(Array.from([1, 2, 3], x => console.log(x)));
Your answer uses 2 for loops inside each other. Which is Bad for Scalability O(n^2). According to BIG O notations.BIG O Cheatsheet
– Arnas Dičkus
yesterday
You must understand that " for (i = 0; i < string.length; i++) { if (string[i] === ' ') continue; result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); }" is ALSO loop in twice. ;) When you call this line : Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); you work with iterator.
– Nikola Lukic
yesterday
What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));
– Nikola Lukic
yesterday
1
Thanks, for explanation I wasn't aware of this.
– Arnas Dičkus
yesterday
2
"String.fromCharCode(str.charCodeAt(x) ^ 32)
" is too much magic. It's a nice trick, but works only on ASCII characters. Don't do that. There's no reason not to use.toUpperCase()
.
– Bergi
23 hours ago
|
show 3 more comments
I use tradicional js. This works on 99% off today browsers.
Where answer very pragmatic. I use array access for string ;
Magic is "String.fromCharCode(str.charCodeAt(x) ^ 32);"
Make it inverse always when we call this line.
var index = 0;
var mydata= "hello";
function wave (str){
var FINAL = [];
var newString = "";
for (var j =0; j < str.length; j++) {
for (var x =0; x < str.length; x++) {
var rez = "";
if (x == index) {
rez = String.fromCharCode(str.charCodeAt(x) ^ 32);
} else {
rez = str[x];
}
newString += rez ;
}
FINAL.push(newString);
newString = "";
index++;
}
return FINAL;
}
var rezArray = wave(mydata);
console.log(rezArray);
// Just for teory
console.log(Array.from([1, 2, 3], x => console.log(x)));
I use tradicional js. This works on 99% off today browsers.
Where answer very pragmatic. I use array access for string ;
Magic is "String.fromCharCode(str.charCodeAt(x) ^ 32);"
Make it inverse always when we call this line.
var index = 0;
var mydata= "hello";
function wave (str){
var FINAL = [];
var newString = "";
for (var j =0; j < str.length; j++) {
for (var x =0; x < str.length; x++) {
var rez = "";
if (x == index) {
rez = String.fromCharCode(str.charCodeAt(x) ^ 32);
} else {
rez = str[x];
}
newString += rez ;
}
FINAL.push(newString);
newString = "";
index++;
}
return FINAL;
}
var rezArray = wave(mydata);
console.log(rezArray);
// Just for teory
console.log(Array.from([1, 2, 3], x => console.log(x)));
var index = 0;
var mydata= "hello";
function wave (str){
var FINAL = [];
var newString = "";
for (var j =0; j < str.length; j++) {
for (var x =0; x < str.length; x++) {
var rez = "";
if (x == index) {
rez = String.fromCharCode(str.charCodeAt(x) ^ 32);
} else {
rez = str[x];
}
newString += rez ;
}
FINAL.push(newString);
newString = "";
index++;
}
return FINAL;
}
var rezArray = wave(mydata);
console.log(rezArray);
// Just for teory
console.log(Array.from([1, 2, 3], x => console.log(x)));
var index = 0;
var mydata= "hello";
function wave (str){
var FINAL = [];
var newString = "";
for (var j =0; j < str.length; j++) {
for (var x =0; x < str.length; x++) {
var rez = "";
if (x == index) {
rez = String.fromCharCode(str.charCodeAt(x) ^ 32);
} else {
rez = str[x];
}
newString += rez ;
}
FINAL.push(newString);
newString = "";
index++;
}
return FINAL;
}
var rezArray = wave(mydata);
console.log(rezArray);
// Just for teory
console.log(Array.from([1, 2, 3], x => console.log(x)));
edited yesterday
answered yesterday
Nikola LukicNikola Lukic
1,75121835
1,75121835
Your answer uses 2 for loops inside each other. Which is Bad for Scalability O(n^2). According to BIG O notations.BIG O Cheatsheet
– Arnas Dičkus
yesterday
You must understand that " for (i = 0; i < string.length; i++) { if (string[i] === ' ') continue; result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); }" is ALSO loop in twice. ;) When you call this line : Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); you work with iterator.
– Nikola Lukic
yesterday
What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));
– Nikola Lukic
yesterday
1
Thanks, for explanation I wasn't aware of this.
– Arnas Dičkus
yesterday
2
"String.fromCharCode(str.charCodeAt(x) ^ 32)
" is too much magic. It's a nice trick, but works only on ASCII characters. Don't do that. There's no reason not to use.toUpperCase()
.
– Bergi
23 hours ago
|
show 3 more comments
Your answer uses 2 for loops inside each other. Which is Bad for Scalability O(n^2). According to BIG O notations.BIG O Cheatsheet
– Arnas Dičkus
yesterday
You must understand that " for (i = 0; i < string.length; i++) { if (string[i] === ' ') continue; result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); }" is ALSO loop in twice. ;) When you call this line : Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); you work with iterator.
– Nikola Lukic
yesterday
What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));
– Nikola Lukic
yesterday
1
Thanks, for explanation I wasn't aware of this.
– Arnas Dičkus
yesterday
2
"String.fromCharCode(str.charCodeAt(x) ^ 32)
" is too much magic. It's a nice trick, but works only on ASCII characters. Don't do that. There's no reason not to use.toUpperCase()
.
– Bergi
23 hours ago
Your answer uses 2 for loops inside each other. Which is Bad for Scalability O(n^2). According to BIG O notations.BIG O Cheatsheet
– Arnas Dičkus
yesterday
Your answer uses 2 for loops inside each other. Which is Bad for Scalability O(n^2). According to BIG O notations.BIG O Cheatsheet
– Arnas Dičkus
yesterday
You must understand that " for (i = 0; i < string.length; i++) { if (string[i] === ' ') continue; result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); }" is ALSO loop in twice. ;) When you call this line : Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); you work with iterator.
– Nikola Lukic
yesterday
You must understand that " for (i = 0; i < string.length; i++) { if (string[i] === ' ') continue; result.push(Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); }" is ALSO loop in twice. ;) When you call this line : Array.from(string, (c, j) => i === j ? c.toUpperCase() : c).join('')); you work with iterator.
– Nikola Lukic
yesterday
What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));
– Nikola Lukic
yesterday
What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));
– Nikola Lukic
yesterday
1
1
Thanks, for explanation I wasn't aware of this.
– Arnas Dičkus
yesterday
Thanks, for explanation I wasn't aware of this.
– Arnas Dičkus
yesterday
2
2
"
String.fromCharCode(str.charCodeAt(x) ^ 32)
" is too much magic. It's a nice trick, but works only on ASCII characters. Don't do that. There's no reason not to use .toUpperCase()
.– Bergi
23 hours ago
"
String.fromCharCode(str.charCodeAt(x) ^ 32)
" is too much magic. It's a nice trick, but works only on ASCII characters. Don't do that. There's no reason not to use .toUpperCase()
.– Bergi
23 hours ago
|
show 3 more comments
I use modify of String prototype :
for implementation of replaceAt .
// Modify prototype
String.prototype.replaceAt=function(index, replacement) {
return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
}
function WaveFunction(str) {
var base = str;
var R = [];
var n =str.length;
for (var i=0 ; i<n ;i++){
str = base;
var c=str.charAt(i);
var res = c.toUpperCase();
// console.log(res);
str = str.replaceAt(i, res);
R.push(str);
}
return R;
}
var REZ = WaveFunction("hello");
console.log(REZ);
1
Nice solution with replaceAt !
– Nikola Lukic
yesterday
where isreplaceAt
from?
– Nina Scholz
yesterday
@NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.
– Arnas Dičkus
yesterday
1
String.prototype.replaceAt=function(index, replacement) { return this.substr(0, index) + replacement+ this.substr(index + replacement.length); } I am sorry i forgot to add it to my answer :)
– Peter Hassaballah
yesterday
1
Don't modify objects you don't own.
– Emile Bergeron
yesterday
|
show 1 more comment
I use modify of String prototype :
for implementation of replaceAt .
// Modify prototype
String.prototype.replaceAt=function(index, replacement) {
return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
}
function WaveFunction(str) {
var base = str;
var R = [];
var n =str.length;
for (var i=0 ; i<n ;i++){
str = base;
var c=str.charAt(i);
var res = c.toUpperCase();
// console.log(res);
str = str.replaceAt(i, res);
R.push(str);
}
return R;
}
var REZ = WaveFunction("hello");
console.log(REZ);
1
Nice solution with replaceAt !
– Nikola Lukic
yesterday
where isreplaceAt
from?
– Nina Scholz
yesterday
@NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.
– Arnas Dičkus
yesterday
1
String.prototype.replaceAt=function(index, replacement) { return this.substr(0, index) + replacement+ this.substr(index + replacement.length); } I am sorry i forgot to add it to my answer :)
– Peter Hassaballah
yesterday
1
Don't modify objects you don't own.
– Emile Bergeron
yesterday
|
show 1 more comment
I use modify of String prototype :
for implementation of replaceAt .
// Modify prototype
String.prototype.replaceAt=function(index, replacement) {
return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
}
function WaveFunction(str) {
var base = str;
var R = [];
var n =str.length;
for (var i=0 ; i<n ;i++){
str = base;
var c=str.charAt(i);
var res = c.toUpperCase();
// console.log(res);
str = str.replaceAt(i, res);
R.push(str);
}
return R;
}
var REZ = WaveFunction("hello");
console.log(REZ);
I use modify of String prototype :
for implementation of replaceAt .
// Modify prototype
String.prototype.replaceAt=function(index, replacement) {
return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
}
function WaveFunction(str) {
var base = str;
var R = [];
var n =str.length;
for (var i=0 ; i<n ;i++){
str = base;
var c=str.charAt(i);
var res = c.toUpperCase();
// console.log(res);
str = str.replaceAt(i, res);
R.push(str);
}
return R;
}
var REZ = WaveFunction("hello");
console.log(REZ);
// Modify prototype
String.prototype.replaceAt=function(index, replacement) {
return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
}
function WaveFunction(str) {
var base = str;
var R = [];
var n =str.length;
for (var i=0 ; i<n ;i++){
str = base;
var c=str.charAt(i);
var res = c.toUpperCase();
// console.log(res);
str = str.replaceAt(i, res);
R.push(str);
}
return R;
}
var REZ = WaveFunction("hello");
console.log(REZ);
// Modify prototype
String.prototype.replaceAt=function(index, replacement) {
return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
}
function WaveFunction(str) {
var base = str;
var R = [];
var n =str.length;
for (var i=0 ; i<n ;i++){
str = base;
var c=str.charAt(i);
var res = c.toUpperCase();
// console.log(res);
str = str.replaceAt(i, res);
R.push(str);
}
return R;
}
var REZ = WaveFunction("hello");
console.log(REZ);
edited yesterday
Nikola Lukic
1,75121835
1,75121835
answered yesterday
Peter HassaballahPeter Hassaballah
825
825
1
Nice solution with replaceAt !
– Nikola Lukic
yesterday
where isreplaceAt
from?
– Nina Scholz
yesterday
@NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.
– Arnas Dičkus
yesterday
1
String.prototype.replaceAt=function(index, replacement) { return this.substr(0, index) + replacement+ this.substr(index + replacement.length); } I am sorry i forgot to add it to my answer :)
– Peter Hassaballah
yesterday
1
Don't modify objects you don't own.
– Emile Bergeron
yesterday
|
show 1 more comment
1
Nice solution with replaceAt !
– Nikola Lukic
yesterday
where isreplaceAt
from?
– Nina Scholz
yesterday
@NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.
– Arnas Dičkus
yesterday
1
String.prototype.replaceAt=function(index, replacement) { return this.substr(0, index) + replacement+ this.substr(index + replacement.length); } I am sorry i forgot to add it to my answer :)
– Peter Hassaballah
yesterday
1
Don't modify objects you don't own.
– Emile Bergeron
yesterday
1
1
Nice solution with replaceAt !
– Nikola Lukic
yesterday
Nice solution with replaceAt !
– Nikola Lukic
yesterday
where is
replaceAt
from?– Nina Scholz
yesterday
where is
replaceAt
from?– Nina Scholz
yesterday
@NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.
– Arnas Dičkus
yesterday
@NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.
– Arnas Dičkus
yesterday
1
1
String.prototype.replaceAt=function(index, replacement) { return this.substr(0, index) + replacement+ this.substr(index + replacement.length); } I am sorry i forgot to add it to my answer :)
– Peter Hassaballah
yesterday
String.prototype.replaceAt=function(index, replacement) { return this.substr(0, index) + replacement+ this.substr(index + replacement.length); } I am sorry i forgot to add it to my answer :)
– Peter Hassaballah
yesterday
1
1
Don't modify objects you don't own.
– Emile Bergeron
yesterday
Don't modify objects you don't own.
– Emile Bergeron
yesterday
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54983179%2fcreating-a-wave-of-string-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Shouldn't you convert some characters to upper case?
– Aycan Yaşıt
yesterday