Creating a wave of string 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
13 hours ago
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 13 hours ago
vahdet
1,97531330
1,97531330
asked 13 hours ago
Arnas DičkusArnas Dičkus
7718
7718
Shouldn't you convert some characters to upper case?
– Aycan Yaşıt
13 hours ago
add a comment |
Shouldn't you convert some characters to upper case?
– Aycan Yaşıt
13 hours ago
Shouldn't you convert some characters to upper case?
– Aycan Yaşıt
13 hours ago
Shouldn't you convert some characters to upper case?
– Aycan Yaşıt
13 hours ago
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
12 hours ago
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
12 hours ago
What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));
– Nikola Lukic
12 hours ago
1
Thanks, for explanation I wasn't aware of this.
– Arnas Dičkus
12 hours ago
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
7 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
12 hours ago
where isreplaceAt
from?
– Nina Scholz
12 hours ago
@NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.
– Arnas Dičkus
12 hours ago
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
11 hours ago
1
Don't modify objects you don't own.
– Emile Bergeron
11 hours ago
|
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 13 hours ago
Nina ScholzNina Scholz
189k1598172
189k1598172
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 13 hours ago
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 13 hours ago
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
12 hours ago
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
12 hours ago
What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));
– Nikola Lukic
12 hours ago
1
Thanks, for explanation I wasn't aware of this.
– Arnas Dičkus
12 hours ago
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
7 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
12 hours ago
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
12 hours ago
What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));
– Nikola Lukic
12 hours ago
1
Thanks, for explanation I wasn't aware of this.
– Arnas Dičkus
12 hours ago
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
7 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 12 hours ago
answered 12 hours ago
Nikola LukicNikola Lukic
1,74521835
1,74521835
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
12 hours ago
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
12 hours ago
What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));
– Nikola Lukic
12 hours ago
1
Thanks, for explanation I wasn't aware of this.
– Arnas Dičkus
12 hours ago
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
7 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
12 hours ago
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
12 hours ago
What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));
– Nikola Lukic
12 hours ago
1
Thanks, for explanation I wasn't aware of this.
– Arnas Dičkus
12 hours ago
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
7 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
12 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
12 hours ago
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
12 hours ago
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
12 hours ago
What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));
– Nikola Lukic
12 hours ago
What you think what is it : console.log(Array.from([1, 2, 3], x => console.log(x)));
– Nikola Lukic
12 hours ago
1
1
Thanks, for explanation I wasn't aware of this.
– Arnas Dičkus
12 hours ago
Thanks, for explanation I wasn't aware of this.
– Arnas Dičkus
12 hours ago
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
7 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
7 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
12 hours ago
where isreplaceAt
from?
– Nina Scholz
12 hours ago
@NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.
– Arnas Dičkus
12 hours ago
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
11 hours ago
1
Don't modify objects you don't own.
– Emile Bergeron
11 hours ago
|
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
12 hours ago
where isreplaceAt
from?
– Nina Scholz
12 hours ago
@NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.
– Arnas Dičkus
12 hours ago
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
11 hours ago
1
Don't modify objects you don't own.
– Emile Bergeron
11 hours ago
|
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 11 hours ago
Nikola Lukic
1,74521835
1,74521835
answered 13 hours ago
Peter HassaballahPeter Hassaballah
825
825
1
Nice solution with replaceAt !
– Nikola Lukic
12 hours ago
where isreplaceAt
from?
– Nina Scholz
12 hours ago
@NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.
– Arnas Dičkus
12 hours ago
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
11 hours ago
1
Don't modify objects you don't own.
– Emile Bergeron
11 hours ago
|
show 1 more comment
1
Nice solution with replaceAt !
– Nikola Lukic
12 hours ago
where isreplaceAt
from?
– Nina Scholz
12 hours ago
@NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.
– Arnas Dičkus
12 hours ago
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
11 hours ago
1
Don't modify objects you don't own.
– Emile Bergeron
11 hours ago
1
1
Nice solution with replaceAt !
– Nikola Lukic
12 hours ago
Nice solution with replaceAt !
– Nikola Lukic
12 hours ago
where is
replaceAt
from?– Nina Scholz
12 hours ago
where is
replaceAt
from?– Nina Scholz
12 hours ago
@NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.
– Arnas Dičkus
12 hours ago
@NikolaLukic I can seem to get it work I get error str.replaceAt is not a function.
– Arnas Dičkus
12 hours ago
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
11 hours ago
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
11 hours ago
1
1
Don't modify objects you don't own.
– Emile Bergeron
11 hours ago
Don't modify objects you don't own.
– Emile Bergeron
11 hours ago
|
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
13 hours ago