Last active
October 26, 2023 19:59
-
-
Save sandrabosk/cd94fedce68ac34ff8b59d0ee7f9f91d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// check-js-loops.md: https://gist.github.com/sandrabosk/6873d93f293080e896adc84dc212cddf | |
// 1 | |
for(let i = 1; i<= 50; i++){ | |
if(i % 5 === 0 && i % 7 === 0 ) console.log("ironhack") | |
else if(i % 5 === 0) console.log("iron") | |
else if( i % 7 === 0 ) console.log("hack") | |
else console.log(i); | |
} | |
// 2: Use any loop. Given the iterable let str='hello,dear.friend! nice,to.see you!', | |
// replace each dot and comma with space. The final output should be: hello dear friend! nice to see you!. | |
let str = 'hello,dear.friend! nice,to.see you!' | |
// version 1: | |
let newStr = ''; | |
for (let char of str){ | |
if(char === '.' || char === ','){ | |
char = ' ' | |
} | |
newStr += char | |
} | |
console.log(newStr); // hello dear friend! nice to see you! | |
console.log('----------------------------------------------------'); | |
// version 2: | |
let stringy = ''; | |
for (let i = 0; i < str.length; i++){ | |
if(str[i] === '.' || str[i] === ','){ | |
stringy += ' '; | |
} else { | |
stringy += str[i]; | |
} | |
} | |
console.log(stringy); // hello dear friend! nice to see you! | |
console.log('----------------------------------------------------'); | |
// version 3: | |
for(let i=0; i < str.length; i++){ | |
if(str[i] === ',' || str[i] === '.'){ | |
str = str.substr(0,i) + ' ' + str.substr(i+1); | |
} | |
} | |
console.log(str); // hello dear friend! nice to see you! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment